简体   繁体   English

如何避免在云存储事件中对文件夹执行Firebase功能

[英]How to avoid performing a firebase function on folders on cloud storage events

I'm trying to organize assets(images) into folders with a unique id for each asset, the reason being that each asset will have multiple formats (thumbnails, and formats optimized for web and different viewports). 我正在尝试将资产(图像)组织到每个资产具有唯一ID的文件夹中,原因是每个资产将具有多种格式(缩略图,以及针对Web和不同视口优化的格式)。

So for every asset that I upload to the folder assets-temp/ is then moved and renamed by the functions into assets/{unique-id}/original{extension} . 因此,对于我上传到文件夹assets-temp/每个资产,然后通过函数将其移动并重命名为assets/{unique-id}/original{extension}

example: assets-temp/my-awesome-image.jpg should become assets/489023840984/original.jpg . 例如: assets-temp/my-awesome-image.jpg应该变成assets/489023840984/original.jpg

note: I also keep track of the files with their original name in the DB and in the original's file metadata. 注意:我还将在数据库和原始文件的元数据中跟踪具有原始名称的文件。

The issue : The function runs and performs what I want, but it also adds a folder named assets/{uuid}/original/ with nothing in it... 问题 :该函数运行并执行我想要的操作,但是它还会添加一个名为assets/{uuid}/original/的文件夹,其中没有任何内容...

The function: 功能:

exports.process_new_assets = functions.storage.object().onFinalize(async (object) => {
  // Run this function only for files uploaded to the "assets-temp/" folder.
  if (!object.name.startsWith('assets-temp/')) return null;

  const file = bucket.file(object.name);
  const fileExt = path.extname(object.name);
  const destination = bucket.file(`assets/${id}/original${fileExt}`);
  const metadata = {
    id,
    name: object.name.split('/').pop()
  };

  // Move the file to the new location.
  return file.move(destination, {metadata});
});

I am guessing that this might happen if the operation of uploading the original image triggers two separate events: one that creates the directory assets-temp and one that creates the file assets-temp/my-awesome-image.jpg. 猜想 ,如果上传原始图像的操作触发两个单独的事件,可能会发生这种情况:一个创建目录资产临时目录,另一个创建文件资产临时目录/my-awesome-image.jpg。

If I guessed right, the first operation will trigger your function with a directory object (named "assets-temp/" ). 如果我猜对了,第一个操作将使用目录对象(名为"assets-temp/" )触发您的函数。 This matches your first if , so the code will proceed and do 这与您的第一个if匹配,因此代码将继续执行

destination = bucket.file('assets/${id}/original') // fileExt being empty

and then call file.move - this will create assets/id/original/ directory. 然后调用file.move-这将创建assets/id/original/目录。

Simply improve your 'if' to exclude a file named "assets-temp/". 只需改进您的“ if”,即可排除名为“ assets-temp /”的文件。

According to the documentation there is no such thing as folders in cloud storage, however, it is possible to emulate them, like you can do by using the console GUI. 根据文档 ,云存储中没有文件夹之类的东西,但是可以像使用控制台GUI一样模拟它们。 When creating folders what really happens is that an empty object is created(zero bytes of space) but its name ends with a forward slash, also folder names can end with _$folder$ but it is my understanding that that is how things worked in older versions so for newer buckets the forward slash is enough. 创建文件夹时,真正发生的事情是创建了一个空对象(零字节空间),但其名称以正斜杠结尾,文件夹名称也可以以_$folder$结尾,但是据我了解,这就是工作方式较旧的版本,因此对于较新的存储桶,正斜杠就足够了。

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

相关问题 如何使用 Cloud Function 从 Cloud Storage 获取 Firebase 的 object? - How to get object from Cloud Storage for Firebase using Cloud Function? 如何从 Firebase 云 Function 中删除 Firebase 存储文件夹? - How to delete a Firebase Storage folder from a Firebase Cloud Function? 从Cloud功能的Firebase存储下载时出错 - Error downloading from firebase Storage in a Cloud Function 使用Firebase Cloud功能检查存储中是否存在映像 - Check if image exists at storage with firebase cloud function 无法使用云删除 Firebase 存储映像 Function - Unable to delete Firebase Storage Image with Cloud Function Cloud Storage for Firebase 访问错误“admin.storage(...).ref is not a function” - Cloud Storage for Firebase access error “admin.storage(…).ref is not a function” 谷歌云存储的 firebase 云函数 API 错误 - firebase cloud function API Error with Google Cloud Storage 如何使用云功能制作缩略图并将其放置在Firebase存储中的特定文件夹中? - how to make thumbnail and place it in the particular folder in Firebase storage using cloud function? 如何在存储具有完全读/写管理员权限的情况下使Firebase Cloud功能正常运行? - How to make a Firebase Cloud function with full read/write admin powers on storage? 如何将映像从Expressjs更新到Google Cloud Storage / Firebase Storage - How update an image from Expressjs to Google Cloud Storage / Firebase Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM