[英]Is there a way to determine once a cloud function has finished running - either through looping getDownloadURL or messaging?
I am still getting the hang of Firebase and Cloud functions, but here is what I'm trying to figure out.我仍然掌握 Firebase 和 Cloud 函数的窍门,但这就是我想要弄清楚的。
My app has a cloud function that will take a PDF that has been uploaded into a storage bucket and convert it into PNG.我的应用程序有一个云 function,它将采用已上传到存储桶中的 PDF 并将其转换为 PNG。 It doesn't destroy the original PDF, so I am left with both files.
它不会破坏原来的 PDF,所以我留下了两个文件。
The URL for the newly created PNG is then attached to a property on one of our documents in Firestore.新创建的 PNG 的 URL 然后附加到我们在 Firestore 中的一个文档的属性。
I want to be able to upload a new PDF to use as a replacement image.我希望能够上传新的 PDF 用作替换图片。 I think I am running into a race condition where the cloud function hasn't finished executing by the time I am trying to call
updateDoc()
with the new PNG.我想我遇到了一个竞争条件,当我尝试用新的 PNG 调用
updateDoc()
时,云 function 还没有完成执行。
On the client side, I have the storageRef
returned from the upload method:在客户端,我有从上传方法返回的
storageRef
:
uploadFunction(...).then((snapshot) => {
return snapshot.ref
}
I'm saving the result of this function to a variable, and I am trying to pass that into the update method that will adjust the property on my document in Firestore:我将这个 function 的结果保存到一个变量中,我试图将其传递到更新方法中,该方法将调整我在 Firestore 中文档的属性:
const storageRef = await functionThatUploadsPDF(file);
updateDocumentInFirestore(storageRef);
Within updateDocumentInFirestore
, I'm trying to navigate to the new reference that should exist once the cloud function has finished, get a download URL, and update that property on my document:在
updateDocumentInFirestore
,我试图导航到云 function 完成后应该存在的新引用,下载 URL,并更新我文档中的该属性:
const newImageRef = ref(storageRef.parent, "generatedImage.png");
const newDownloadURL = getDownloadURL(newImageRef).then((url) => {
updateDoc(documentRef, backgroundImage: url);
});
However, I am getting the following error - I believe due to the cloud function having not finished yet:但是,我收到以下错误 - 我相信是由于云 function 尚未完成:
Firebase Storage: Object 'storage-bucket/generatedImage.png' does not exist.
Firebase 存储:Object 'storage-bucket/generatedImage.png' 不存在。 (storage/object-not-found)
(存储/未找到对象)
I could try to poll the storage for the existence of generatedImage.png
until the getDownloadURL
call returns an actual URL, but I worry about the amount of calls this would yield.我可以尝试轮询存储以了解
generatedImage.png
是否存在,直到getDownloadURL
调用返回实际的 URL,但我担心这会产生调用量。
If there is a way for the cloud function to send a message to let me know that the conversion is finished, I can send a call once for the download URL after receiving said message.如果有办法让云端function发消息告诉我转换完成,我收到消息后可以发一次电话下载URL。 However, I can't figure out how to accomplish this.
但是,我无法弄清楚如何实现这一点。
I have been pursuing course 1. So far, but have not met any success yet.我一直在追求课程 1。到目前为止,但还没有取得任何成功。 Scouring through Firebase documentation, I haven't been able to find any supporting resources on how to accomplish 1 or 2. Does anyone have any suggestions - either on my planned courses of action, or a new option that I haven't considered?
通过 Firebase 文档搜索,我无法找到任何关于如何完成 1 或 2 的支持资源。有没有人有任何建议 - 无论是关于我计划的行动方案,还是我没有考虑过的新选项?
You can use this onFinalize trigger to send a message or update a document in Firestore to indicate that the function has finished running.您可以使用此onFinalize触发器发送消息或更新 Firestore 中的文档以指示 function 已完成运行。 This trigger is triggered whenever a file is created or updated.
每当创建或更新文件时都会触发此触发器。
onFinalize Sent when a new object (or a new generation of an existing object) is successfully created in the bucket.
onFinalize 在桶中成功创建新的 object(或新一代现有对象)时发送。 This includes copying or rewriting an existing object. A failed upload does not trigger this event.
这包括复制或重写现有的 object。上传失败不会触发此事件。
you can also create a promise that resolves when the downloadURL is not null, and use that promise in your updateDocumentInFirestore function. This way, the updateDoc function will only be called once the downloadURL is available.您还可以创建一个 promise,当 downloadURL 不是 null 时解析,并在您的 updateDocumentInFirestore function 中使用该 promise。这样,updateDoc function 只有在 downloadURL 可用时才会被调用。
Additionally, as was mentioned in the comments, you can consider cloud workflow .The exact implementation will depend on your specific use case此外,正如评论中提到的,您可以考虑云工作流。具体实施将取决于您的具体用例
You can also check these similar cases您还可以查看这些类似案例
Firebase Storage: Object does not exist Firebase 存储:Object 不存在
Error: storage/object-not-found when trying to upload large image file 错误:尝试上传大图像文件时未找到存储/对象
Firebase Storage Put could not get object Firebase Storage Put 无法获取 object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.