forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(storage): add Object Contexts samples and system tests #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d749e13
Feat(storage): add Object Contexts samples and system tests
thiyaguk09 19f1454
code refactor
thiyaguk09 58c1c2a
fix: typo corrections
thiyaguk09 1349ee8
Merge branch 'main' into feat-object-contexts
thiyaguk09 f27e22e
test(storage): refactor to state-based assertions
thiyaguk09 fe02862
Merge branch 'main' into feat-object-contexts
thiyaguk09 fd1aed3
feat(storage): add samples and system tests for bucket encryption enf…
thiyaguk09 5ab3051
Merge branch 'main' into feat-object-contexts
iennae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Get Object Contexts | ||
| // description: Retrieves the structured Object Contexts from an object. | ||
| // usage: node getObjectContexts.js <BUCKET_NAME> <FILE_NAME> | ||
|
|
||
| /** | ||
| * This application demonstrates how to retrieve the 'contexts' field from a file | ||
| * in Google Cloud Storage. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket', fileName = 'test.txt') { | ||
| // [START storage_get_object_contexts] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of your GCS file | ||
| // const fileName = 'your-file-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function getObjectContexts() { | ||
| // Gets the metadata for the file | ||
| const [metadata] = await storage | ||
| .bucket(bucketName) | ||
| .file(fileName) | ||
| .getMetadata(); | ||
|
|
||
| // Contexts are stored in metadata.contexts.custom | ||
| if (metadata.contexts && metadata.contexts.custom) { | ||
| console.log(`Object Contexts for ${fileName}:`); | ||
|
|
||
| // Iterate through the custom contexts to show values and timestamps | ||
| for (const [key, details] of Object.entries(metadata.contexts.custom)) { | ||
| console.log(`- Key: ${key}`); | ||
| console.log(` Value: ${details.value}`); | ||
| console.log(` Created: ${details.createTime}`); | ||
| console.log(` Updated: ${details.updateTime}`); | ||
| } | ||
| } else { | ||
| console.log(`No Object Contexts found for ${fileName}.`); | ||
| } | ||
| } | ||
|
|
||
| getObjectContexts().catch(console.error); | ||
| // [END storage_get_object_contexts] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: List Objects with Context Filter | ||
| // description: Lists objects in a bucket that match specific custom contexts. | ||
| // usage: node listObjectsWithContextFilter.js <BUCKET_NAME> | ||
|
thiyaguk09 marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * This application demonstrates how to list objects in a bucket while filtering | ||
| * by their custom 'contexts' metadata. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket') { | ||
| // [START storage_list_object_contexts] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function listObjectContexts() { | ||
| // Define the filter for contexts. | ||
| const bucket = storage.bucket(bucketName); | ||
|
|
||
| /** | ||
| * List any object that has a context with the specified key attached. | ||
| * Syntax: contexts."<key>"="<value>" | ||
| */ | ||
| const filterByValue = 'contexts."priority"="high"'; | ||
| const [filesByValue] = await bucket.getFiles({ | ||
| filter: filterByValue, | ||
| }); | ||
|
|
||
| console.log(`\nFiles matching filter [${filterByValue}]:`); | ||
| filesByValue.forEach(file => console.log(` - ${file.name}`)); | ||
|
|
||
| /** | ||
| * List any object that has a context with the specified key attached. | ||
| * Syntax: contexts."<key>":* | ||
| */ | ||
| const filterByExistence = 'contexts."team-owner":*'; | ||
| const [filesWithKey] = await bucket.getFiles({ | ||
| filter: filterByExistence, | ||
| }); | ||
|
|
||
| console.log( | ||
| `\nFiles with the "team-owner" context key [${filterByExistence}]:` | ||
| ); | ||
| filesWithKey.forEach(file => console.log(` - ${file.name}`)); | ||
|
|
||
| /** | ||
| * List any object that does not have a context with the specified key and value attached. | ||
| * Syntax: -contexts."<key>"="<value>" | ||
| */ | ||
| const absenceOfValuePair = '-contexts."priority"="high"'; | ||
| const [filesNoHighPriority] = await bucket.getFiles({ | ||
| filter: absenceOfValuePair, | ||
| }); | ||
|
|
||
| console.log( | ||
| `\nFiles matching absence of value pair [${absenceOfValuePair}]:` | ||
| ); | ||
| filesNoHighPriority.forEach(file => console.log(` - ${file.name}`)); | ||
|
|
||
| /** | ||
| * List any object that that does not have a context with the specified key attached. | ||
| * Syntax: -contexts."<key>":* | ||
| */ | ||
| const absenceOfKey = '-contexts."team-owner":*'; | ||
| const [filesNoTeamOwner] = await bucket.getFiles({ | ||
| filter: absenceOfKey, | ||
| }); | ||
|
|
||
| console.log( | ||
| `\nFiles matching absence of key regardless of value [${absenceOfKey}]:` | ||
| ); | ||
| filesNoTeamOwner.forEach(file => console.log(` - ${file.name}`)); | ||
| } | ||
|
|
||
| listObjectContexts().catch(console.error); | ||
| // [END storage_list_object_contexts] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Set Object Contexts | ||
| // description: Sets custom metadata (contexts) on an object. | ||
| // usage: node setObjectContexts.js <BUCKET_NAME> <FILE_NAME> | ||
|
|
||
| /** | ||
| * This application demonstrates how to set, update, and delete object contexts (metadata) on a file | ||
| * in Google Cloud Storage. | ||
| */ | ||
|
|
||
| function main(bucketName = 'my-bucket', fileName = 'test.txt') { | ||
| // [START storage_set_object_contexts] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of your GCS file | ||
| // const fileName = 'your-file-name'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function setObjectContexts() { | ||
| const file = storage.bucket(bucketName).file(fileName); | ||
| try { | ||
| // Create/Update Object Contexts | ||
| // Object Contexts live in the 'contexts' field, not the 'metadata' field. | ||
| const [metadata] = await file.setMetadata({ | ||
| contexts: { | ||
| custom: { | ||
| 'team-owner': {value: 'storage-team'}, | ||
| priority: {value: 'high'}, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| console.log(`Updated Object Contexts for ${fileName}:`); | ||
| console.log(JSON.stringify(metadata.contexts, null, 2)); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing set object contexts:', | ||
| error.message || error | ||
| ); | ||
| } | ||
|
thiyaguk09 marked this conversation as resolved.
Outdated
|
||
| // Delete a specific key from the context: | ||
| // We send 'null' for the specific key we want to remove. | ||
| await file.setMetadata({ | ||
| contexts: { | ||
| custom: { | ||
| 'team-owner': null, | ||
| }, | ||
| }, | ||
| }); | ||
| console.log(`Deleted 'team-owner' key from contexts for ${fileName}.`); | ||
|
|
||
| // Delete all keys from the context: | ||
| // We set the 'custom' property to null to wipe the entire map. | ||
| await file.setMetadata({ | ||
| contexts: { | ||
| custom: null, | ||
| }, | ||
| }); | ||
| console.log(`Cleared all custom contexts for ${fileName}.`); | ||
|
thiyaguk09 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| setObjectContexts().catch(console.error); | ||
| // [END storage_set_object_contexts] | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.