* This application demonstrates how to retrieve the 'contexts' field from 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_get_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 getObjectContexts() { |
| 45 | // Gets the metadata for the file |
| 46 | const [metadata] = await storage |
| 47 | .bucket(bucketName) |
| 48 | .file(fileName) |
| 49 | .getMetadata(); |
| 50 | |
| 51 | // Contexts are stored in metadata.contexts.custom |
| 52 | if (metadata.contexts && metadata.contexts.custom) { |
| 53 | console.log(`Object Contexts for ${fileName}:`); |
| 54 | |
| 55 | // Iterate through the custom contexts to show values and timestamps |
| 56 | for (const [key, details] of Object.entries(metadata.contexts.custom)) { |
| 57 | console.log(`- Key: ${key}`); |
| 58 | console.log(` Value: ${details.value}`); |
| 59 | console.log(` Created: ${details.createTime}`); |
| 60 | console.log(` Updated: ${details.updateTime}`); |
| 61 | } |
| 62 | } else { |
| 63 | console.log(`No Object Contexts found for ${fileName}.`); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | getObjectContexts().catch(console.error); |
| 68 | // [END storage_get_object_contexts] |
| 69 | } |
| 70 | |
| 71 | main(...process.argv.slice(2)); |
no test coverage detected