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

zee*_*zee 1 javascript metadata google-cloud-storage google-cloud-functions

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

我直接从谷歌的文档中复制了代码

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

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
  });
Run Code Online (Sandbox Code Playgroud)

我希望设置元数据,但它返回未定义。

zee*_*zee 5

我想到了。它一定要是:

await bucket.upload(tempFilePath, { destination: filePath, metadata: { metadata: metadata } });

谷歌在https://firebase.google.com/docs/storage/extend-with-functions上的文档没有提到这一点。

  • 这是事实,也是可笑的。 (2认同)