MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

storage/listObjectContexts.js:27–102  ·  view source on GitHub ↗

* This application demonstrates how to list objects in a bucket while filtering * by their custom 'contexts' metadata.

(bucketName = 'my-bucket')

Source from the content-addressed store, hash-verified

25 */
26
27function main(bucketName = 'my-bucket') {
28 // [START storage_list_object_contexts]
29 /**
30 * TODO(developer): Uncomment the following lines before running the sample.
31 */
32 // The ID of your GCS bucket
33 // const bucketName = 'your-unique-bucket-name';
34
35 // Imports the Google Cloud client library
36 const {Storage} = require('@google-cloud/storage');
37
38 // Creates a client
39 const storage = new Storage();
40
41 async function listObjectContexts() {
42 // Define the filter for contexts.
43 const bucket = storage.bucket(bucketName);
44
45 /**
46 * List any object that has a context with the specified key and value.
47 * Syntax: contexts."<key>"="<value>"
48 */
49 const filterByValue = 'contexts."priority"="high"';
50 const [filesByValue] = await bucket.getFiles({
51 filter: filterByValue,
52 });
53
54 console.log(`\nFiles matching filter [${filterByValue}]:`);
55 filesByValue.forEach(file => console.log(` - ${file.name}`));
56
57 /**
58 * List any object that has a context with the specified key attached.
59 * Syntax: contexts."<key>":*
60 */
61 const filterByExistence = 'contexts."team-owner":*';
62 const [filesWithKey] = await bucket.getFiles({
63 filter: filterByExistence,
64 });
65
66 console.log(
67 `\nFiles with the "team-owner" context key [${filterByExistence}]:`
68 );
69 filesWithKey.forEach(file => console.log(` - ${file.name}`));
70
71 /**
72 * List any object that does not have a context with the specified key and value attached.
73 * Syntax: -contexts."<key>"="<value>"
74 */
75 const absenceOfValuePair = '-contexts."priority"="high"';
76 const [filesNoHighPriority] = await bucket.getFiles({
77 filter: absenceOfValuePair,
78 });
79
80 console.log(
81 `\nFiles matching absence of value pair [${absenceOfValuePair}]:`
82 );
83 filesNoHighPriority.forEach(file => console.log(` - ${file.name}`));
84

Callers 1

Calls 1

listObjectContextsFunction · 0.85

Tested by

no test coverage detected