在Firebase Cloud Functions中获取存储元数据

4
在Firebase的Cloud Functions中是否可以获取自定义存储元数据?例如在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();

我想在云函数中检索自定义元数据值。
3个回答

2

使用这个。

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

1
这实际上是正确的方法。Firebase文档在这方面真的很糟糕,但在SDK文档中,我至少可以找到一个解释,为什么要访问meta [0]meta [0]文件元数据,而meta [1]完整的API响应。两者都是对象,你仍然需要知道你需要访问meta [0]["metadata"]来获取包含你自定义元数据的对象。 - sceee

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'];

.......

0

你有没有如何做到这点的例子? - Abcideff Ahmad
没有什么方便的东西,但API文档非常简单明了,所以你应该能够进行实验。 - Doug Stevenson

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