[英]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.