【问题标题】:How to change file's metadata in Google Cloud Storage with Node.js如何使用 Node.js 更改 Google Cloud Storage 中的文件元数据
【发布时间】:2019-08-22 09:22:37
【问题描述】:

我的图片(托管在 Google Cloud Storage 中)的元数据具有名为 downloaded 的属性,如果已下载图片,则 downloaded 键中的值将从 0 更改为 1

https://cloud.google.com/storage/docs/viewing-editing-metadata#storage-view-object-metadata-nodejs 中的代码显示了如何查看元数据,但并未真正介绍如何更改元数据。

可以吗?

【问题讨论】:

    标签: 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}`)
    

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 2021-07-11
      • 2020-11-14
      • 2018-09-16
      • 2023-03-13
      • 2015-08-01
      相关资源
      最近更新 更多