【问题标题】:How to change file's metadata in Google Cloud Storage with Node.js如何使用 Node.js 更改 Google Cloud Storage 中的文件元数据
【发布时间】:2019-08-22 09:22:37
【问题描述】:
【问题讨论】:
标签:
javascript
node.js
google-cloud-platform
google-cloud-storage
【解决方案1】:
是的,有可能。
方法是使用File.setMetadata() 方法。
例如,将元数据添加到 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}`)
要更新它,您可以使用getMetadata() 方法检索当前元数据,对其进行修改,然后使用setMetadata() 方法更新它。
例如:
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}`)