My image's (which is hosted in Google Cloud Storage) metadata has the property named downloaded
, if the image has been downloaded, the value inside the downloaded
key will be changed from 0
to 1
.
The code in shows how to view the metadatas but didn't really cover how to change the metadata.
Is it possible to do so?
My image's (which is hosted in Google Cloud Storage) metadata has the property named downloaded
, if the image has been downloaded, the value inside the downloaded
key will be changed from 0
to 1
.
The code in https://cloud.google./storage/docs/viewing-editing-metadata#storage-view-object-metadata-nodejs shows how to view the metadatas but didn't really cover how to change the metadata.
Is it possible to do so?
Share Improve this question asked Apr 1, 2019 at 8:51 Andre Christoga PramadityaAndre Christoga Pramaditya 712 silver badges11 bronze badges1 Answer
Reset to default 7Yes, it is possible.
The way to do it is by using the File.setMetadata()
method.
For example, to add metadata to an object in GCS:
const file = storage
.bucket(bucketName)
.file(filename)
const metadata = {
metadata: {
example: 'test'
}
}
file.setMetadata(metadata)
// Get the updated Metadata
const get_metadata = file.getMetadata();
// Will print `File: test`
console.log(`File: ${metadata.metadata.example}`)
To update it, you can retrieve the current metadata with the getMetadata()
method, modifying it, and updating it with the setMetadata()
method .
For example:
const storage = new Storage();
const file = storage
.bucket(bucketName)
.file(filename)
// Get the file's metadata
const [metadata] = await file.getMetadata()
console.log(`File: ${metadata.name}`)
// update metadata
file.setMetadata(metadata.metadata.example='updated')
// Get the updated metadata
const [get_metadata] = await file.getMetadata()
console.log(`File: ${get_metadata.metadata.example}`)