繁体   English   中英

如何使用 Cloud Function 从 Cloud Storage 获取 Firebase 的 object?

[英]How to get object from Cloud Storage for Firebase using Cloud Function?

对不起。 我以前提出过同样的问题,但它被认为与这个答案重复。 我真的不明白那里的答案,我不明白为什么这个问题会回答我的问题。 对我来说似乎很不同。 我需要获取图像 object 但答案是创建 stream。 有人可以帮我将那里的答案与我的问题联系起来吗? 我是云 function 的新手。

==================================================== ======================

如果我将 Cloud Functions 用于 Firebase Cloud Storage 触发器。 我可以像这样得到图像 object

exports.compressImage = functions.region("asia-east2").storage.object().onFinalize(async (object) => {
    
   // I can get the `object` easily in here

})

但现在我想使用firestore触发器做一些事情,我想从我的firestore触发器中的存储桶中获取一个object

exports.dbEventsOnCreate = functions..firestore.document(path).onCreate(async (snapshot,context) => {

    // I want to get an image `object` with a specific path from firebase storage bucket in here

})

这是我的图像在 firebase 存储中的路径

gs://xxx.appspot.com/eventPoster/{uid}/{imageID}

在此处输入图像描述

那么如何从我的云 function firestore 触发器中的路径中的存储桶中获取图像 object ?

要处理云 function 上的文件,您可以按照 本指南进行操作,您将在几句话中找到更多详细信息

要下载文件:

// Download file from bucket.
const bucket = admin.storage().bucket(fileBucket);
const tempFilePath = path.join(os.tmpdir(), fileName);
const metadata = {
  contentType: contentType,
};
await bucket.file(filePath).download({destination: tempFilePath});
console.log('Image downloaded locally to', tempFilePath);

这会将图像保存到临时文件夹,因为这是文档中推荐的内容。

使用 gcs.bucket.file(filePath).download 将文件下载到 Cloud Functions 实例上的临时目录。 在此位置,您可以根据需要处理文件。

要上传文件:

await bucket.upload(tempFilePath, {
  destination: thumbFilePath,
  metadata: metadata,
});
// Once the thumbnail has been uploaded delete the local file to free up disk space.
return fs.unlinkSync(tempFilePath);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM