通过Google Cloud函数上传文件时,元数据未保存

时间:2019-08-27 15:12:20

标签: javascript google-cloud-functions google-cloud-storage metadata

我正在使用Firebase云功能调整图像大小,并希望设置自定义元数据。

我直接从google's documentation复制了代码

但是,当我重新读取文件时,它根本没有设置任何元数据。

export const generateThumbs = functions.storage.object().onFinalize(async object => {
  const fileBucket = object.bucket; // The Storage bucket that contains the file.
  const filePath = object.name as string; // File path in the bucket.
  const contentType = object.contentType; // File content type.

  const metadata: any = object.metadata || {};

  // First time around this works and logs metadata.
  // I set "isThumb" as false on client. It is set in 
  // the first run!
  // In the second run, it comes back as undefined
  console.log(object.metadata);

  if (metadata && metadata.isThumb) {
    console.log("Already a thumbnail");
    return;
  }
// .... later
metadata.isThumb = true;

  // Override the original file with the smaller one
  // I am passing it the same metadata, and yet 
  // when the function is triggered again, the 
  // metadata is undefined!  This part is straight from
  // google's documentation. 
  await bucket.upload(tempFilePath, {
    destination: filePath,
    metadata: metadata
  });

我希望可以设置元数据,但是它会以未定义的形式返回。

1 个答案:

答案 0 :(得分:1)

我知道了。必须是:

等待bucket.upload(tempFilePath,{     目的地:filePath,     元数据:{元数据:元数据}   });

位于https://firebase.google.com/docs/storage/extend-with-functions的Google文档没有提及。