最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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}`)
发布评论

评论列表(0)

  1. 暂无评论