【问题标题】:Metadata not saving when uploading a file through Google Cloud functions通过 Google Cloud 功能上传文件时未保存元数据
【发布时间】:2019-08-27 15:12:20
【问题描述】:

我正在使用 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
  });

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

【问题讨论】:

  • console.log(contentType ) 的输出是什么?
  • 在第一次运行中,元数据。在第二次运行中,“未定义”。
  • 根据获取文件元数据部分中的文档customMetadata,您应该使用getMetadata()方法获取元数据,我也没有看到您使用方法updateMetadata()来更新文件元数据。
  • 我直接从谷歌的文档中复制了我的代码。链接在问题中。

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


【解决方案1】:

我想通了。它必须是:

等待 bucket.upload(tempFilePath, { 目的地:文件路径, 元数据:{元数据:元数据} });

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

【讨论】:

  • 这是真的,也很荒谬。
  • 还是这样。
猜你喜欢
  • 2015-08-28
  • 2022-08-19
  • 2021-09-14
  • 1970-01-01
  • 2018-12-06
  • 2020-06-26
  • 2019-06-21
  • 2020-01-29
  • 2020-02-17
相关资源
最近更新 更多