简体   繁体   English

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

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

I am sorry.对不起。 I previously make the same question but it was considered duplicate to this answer .我以前提出过同样的问题,但它被认为与这个答案重复。 I really don't understand the answer there and I don't understand why that question will answer my problem.我真的不明白那里的答案,我不明白为什么这个问题会回答我的问题。 it seems very different to me.对我来说似乎很不同。 I need to get image object but the answer there is to create a stream.我需要获取图像 object 但答案是创建 stream。 could someone please help me to relate the answer from there to my problem?有人可以帮我将那里的答案与我的问题联系起来吗? I am new in cloud function.我是云 function 的新手。 please

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

If I use Cloud Functions for Firebase Cloud Storage triggers.如果我将 Cloud Functions 用于 Firebase Cloud Storage 触发器。 I can get the image object like this我可以像这样得到图像 object

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

})

but now I want do something using firestore trigger, and I want to get an object from my bucket in my firestore trigger但现在我想使用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

})

and here is the path of my image in firebase storage这是我的图像在 firebase 存储中的路径

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

在此处输入图像描述

so how to get image object from bucket in the path like that inside my cloud function firestore trigger?那么如何从我的云 function firestore 触发器中的路径中的存储桶中获取图像 object ?

To handle the file on the cloud function you can follow this guide you will find more detailed info ut in a few words要处理云 function 上的文件,您可以按照 本指南进行操作,您将在几句话中找到更多详细信息

To download a file:要下载文件:

// 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);

That will save the image to a temporary folder since this is what is recomended on the docs.这会将图像保存到临时文件夹,因为这是文档中推荐的内容。

Use gcs.bucket.file(filePath).download to download a file to a temporary directory on your Cloud Functions instance.使用 gcs.bucket.file(filePath).download 将文件下载到 Cloud Functions 实例上的临时目录。 In this location, you can process the file as needed.在此位置,您可以根据需要处理文件。

To upload the file:要上传文件:

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.

相关问题 从Cloud功能的Firebase存储下载时出错 - Error downloading from firebase Storage in a Cloud Function 如何从云存储中获取在某个时间创建的 Firebase 的文件? - how to get files from Cloud Storage for Firebase that created at a certain time? Firebase Cloud function-Get document snapshot field which is the URL of a file in firebase storage and delete the file from firebase storage - js - Firebase Cloud function-Get document snapshot field which is the URL of a file in firebase storage and delete the file from firebase storage - js 当从实时数据库中删除对象时,Firebase功能(用NodeJS编写)从云存储中删除文件 - Firebase function (written in NodeJS) to delete file from Cloud Storage when an object is removed from Realtime Database 如何使用云 function 获取 Firebase 中的实时数据库? - How to get Realtime Database in Firebase by using cloud function? 如何将映像从Expressjs更新到Google Cloud Storage / Firebase Storage - How update an image from Expressjs to Google Cloud Storage / Firebase Storage 如何从 Cloud Function 获取 Firebase 项目名称或 ID - How to get Firebase Project Name or ID from Cloud Function 如何从Cloud Function for Firebase获取条带化付款费用“成功”? - How to get a stripe payment charge 'success' from a Cloud Function for Firebase? 上传到Firebase存储时如何使用Cloud Functions for FIrebase获取mp3文件的持续时间 - How to get the duration of a mp3 file using Cloud Functions for FIrebase when uploaded to firebase storage 如何使用云function检查云存储中的文件路径是否有效? - how to check if a file path in cloud storage is valid or not using cloud function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM