如何使用Node.js在Google Cloud Storage中更改文件的元数据

4
1个回答

6

是的,这是可能的。

实现方法是使用 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}`)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接