* This application demonstrates how to set, update, and delete object contexts (metadata) on a file * in Google Cloud Storage.
(bucketName = 'my-bucket', fileName = 'test.txt')
| 25 | */ |
| 26 | |
| 27 | function main(bucketName = 'my-bucket', fileName = 'test.txt') { |
| 28 | // [START storage_set_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 | // The ID of your GCS file |
| 36 | // const fileName = 'your-file-name'; |
| 37 | |
| 38 | // Imports the Google Cloud client library |
| 39 | const {Storage} = require('@google-cloud/storage'); |
| 40 | |
| 41 | // Creates a client |
| 42 | const storage = new Storage(); |
| 43 | |
| 44 | async function setObjectContexts() { |
| 45 | const file = storage.bucket(bucketName).file(fileName); |
| 46 | |
| 47 | // Create/Update Object Contexts |
| 48 | // Object Contexts live in the 'contexts' field, not the 'metadata' field. |
| 49 | const [metadata] = await file.setMetadata({ |
| 50 | contexts: { |
| 51 | custom: { |
| 52 | 'team-owner': {value: 'storage-team'}, |
| 53 | priority: {value: 'high'}, |
| 54 | }, |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | console.log(`Updated Object Contexts for ${fileName}:`); |
| 59 | console.log(JSON.stringify(metadata.contexts, null, 2)); |
| 60 | } |
| 61 | |
| 62 | setObjectContexts().catch(console.error); |
| 63 | // [END storage_set_object_contexts] |
| 64 | } |
| 65 | |
| 66 | main(...process.argv.slice(2)); |
no test coverage detected