【问题标题】:Get storage metadata in Cloud Functions for Firebase在 Cloud Functions for Firebase 中获取存储元数据
【发布时间】:2017-06-15 05:33:35
【问题描述】:

是否可以在 Cloud Functions for Firebase 中获取自定义存储元数据? 例如在android代码中:

 StorageMetadata metadata = new StorageMetadata.Builder().setContentType("image/jpg")
                        .setCustomMetadata("latitude",String.valueOf(image.latitude))
                        .setCustomMetadata("longitude",String.valueOf(image.longitude))
                        .setCustomMetadata("deviceID",image.deviceID)
                        .setCustomMetadata("userID",image.userID)
                        .setCustomMetadata("deviceFilePath",image.filename)
                        .setCustomMetadata("timestamp",String.valueOf(image.timestamp))
                        .setCustomMetadata("caption",image.caption)
                        .setCustomMetadata("location",image.location)
                        .setCustomMetadata("imageID",key)
                        .build();

我想在云函数中检索自定义元数据值

【问题讨论】:

    标签: node.js firebase google-cloud-functions


    【解决方案1】:

    您可以使用 event.data.metadata 在云函数中获取自定义元数据,如下所示。

    如果你想看完整的例子,这里是链接http://www.zoftino.com/android-firebase-cloud-functions-cloud-storage-trigger-example

    exports.metadata = functions.storage.object().onChange(event => {
    
      //metada
      const fileInfo = event.data;
    
     //custom metadata
      const customMetadata = fileInfo.metadata;
    
       //get each custom metadata value using its key like.. 
      const customMetadataDeviceID = customMetadata['deviceID'];
    
    .......
    

    【讨论】:

      【解决方案2】:

      使用这个。

      }).then(() => {
          return file.getMetadata();
      }).then((meta) => {
          console.log(meta[0]);
          value1 = meta[0]["metadata"]["key1"];
          value2 = meta[0]["metadata"]["key2"];
      

      【讨论】:

      • 这实际上是要走的路。 Firebase 文档在这方面确实很糟糕,但在 SDK 文档中,我至少可以找到一个 explanation 为什么要访问 meta[0]meta[0]The File metadata.meta[1]The full API response. 两者都是对象和你仍然需要知道您需要访问 meta[0]["metadata"] 以获取包含您的自定义元数据的对象。
      【解决方案3】:

      当然,您可以使用Google Cloud Storage Node SDK 来处理存储分区中的文件。使用它来获取指向感兴趣文件的File 对象。然后调用它的getMetadata()方法获取元数据。

      【讨论】:

      • 你有什么例子吗?
      • 没什么好用的,但是 API 文档非常简单,所以你应该可以尝试一下。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 2017-08-15
      • 2018-02-28
      • 2018-12-22
      • 2017-10-02
      • 1970-01-01
      相关资源
      最近更新 更多