我的图片(托管在Google Cloud Storage中)的元数据具有名为downloaded
的属性,如果已下载该图片,则downloaded
键内的值将从0
更改为1
。
https://cloud.google.com/storage/docs/viewing-editing-metadata#storage-view-object-metadata-nodejs中的代码演示了如何查看元数据,但没有涵盖如何更改元数据的内容。
这可能吗?
我的图片(托管在Google Cloud Storage中)的元数据具有名为downloaded
的属性,如果已下载该图片,则downloaded
键内的值将从0
更改为1
。
https://cloud.google.com/storage/docs/viewing-editing-metadata#storage-view-object-metadata-nodejs中的代码演示了如何查看元数据,但没有涵盖如何更改元数据的内容。
这可能吗?
是的,这是可能的。
实现方法是使用 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}`)