[英]Is it possible to scope Firebase functions to a folder inside a storage bucket?
I have tested Firebase functions for storage successfully.我已经成功测试了 Firebase 的存储功能。 However, I havn't seen anywhere a hint how to only invoke the function when a file is added into a folder inside my bucket.
但是,我在任何地方都没有看到提示如何仅在将文件添加到我的存储桶内的文件夹中时调用该函数。 The only hint I have seen about scoping the function is for different buckets here .
我看到的关于确定函数范围的唯一提示是针对不同 的存储桶。
Is it possible to scope the function to a folder inside my bucket, if yes how?是否可以将函数的范围限定在我的存储桶内的文件夹中,如果可以,如何? Or would I need to have multiple buckets instead of folders to separate different tasks.
或者我需要有多个存储桶而不是文件夹来分隔不同的任务。
firebaser here炮兵在这里
There is currently no way to trigger Cloud Functions only for writes in a specific folder in Cloud Storage.当前无法仅针对 Cloud Storage 中特定文件夹中的写入触发 Cloud Functions。 If you want to limit the triggering to a subset of the files in your project, putting them in a separate bucket is currently the only way to accomplish that.
如果您想将触发限制为项目中文件的子集,将它们放在单独的存储桶中是目前实现该目的的唯一方法。
As a workaround, you can write metadata about the image to a supported database (Realtime Database or Cloud Firestore), and use that to trigger a Cloud Function that transforms the file.作为解决方法,您可以将有关图像的元数据写入受支持的数据库(实时数据库或 Cloud Firestore),并使用它来触发转换文件的 Cloud Functions。 This is what I usually do, as it also allows me to capture the metadata in a format that can be queried.
这是我通常做的,因为它还允许我以可以查询的格式捕获元数据。
You may check inside the function.您可以检查函数内部。 Get the "filePath" or "fileDir" on your function and check if it is the folder you want.
在您的函数中获取“filePath”或“fileDir”并检查它是否是您想要的文件夹。
const path = require('path');
const filePath = event.data.name;
const fileDir = path.dirname(filePath);
//if you want to check the posts folder only then:
if (fileDir != 'posts') {
console.log('This is not in post folder');
return null;
}
Please note that Google Cloud Storage works on a flat filesystem.请注意,Google Cloud Storage 在平面文件系统上运行。 So practically there are no directories.
所以实际上没有目录。 If you're storing a file like
/users/profile_pictures/photo.jpg
it is basically all part of the file name.如果您要存储类似
/users/profile_pictures/photo.jpg
的文件,它基本上都是文件名的一部分。 So in reality, there are no directories.所以实际上,没有目录。 There are just files.
只有文件。 Which is why there cannot be a trigger on a directory per se.
这就是为什么目录本身不能有触发器的原因。 Of course you can work that around by checking the name of the file itself and see whether its start matches a particular string or not.
当然,您可以通过检查文件本身的名称并查看其开头是否与特定字符串匹配来解决这个问题。
export const generateThumbnailTrigger = functions.storage.object().onFinalize(async (object) => {
const filePath = object.name;
if (filePath?.startsWith('temp/')) {
console.log('start doing something with the file');
} else {
return false;
}
});
I have tested Firebase functions for storage successfully.我已经成功测试了 Firebase 存储功能。 However, I havn't seen anywhere a hint how to only invoke the function when a file is added into a folder inside my bucket.
但是,我没有在任何地方看到如何仅在将文件添加到存储桶内的文件夹时调用该函数的提示。 The only hint I have seen about scoping the function is for different buckets here .
我已经看到了关于划定范围的功能,唯一的暗示是不同的桶 在这里。
Is it possible to scope the function to a folder inside my bucket , if yes how?是否可以将函数范围限定在我的存储桶内的文件夹中,如果是,如何? Or would I need to have multiple buckets instead of folders to separate different tasks.
或者我是否需要有多个存储桶而不是文件夹来分隔不同的任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.