javascript - How to change file's metadata in Google Cloud Storage with Node.js - Stack Overflow

My image's (which is hosted in Google Cloud Storage) metadata has the property named downloaded, i

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

Yes, 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}`)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745540905a4632122.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信