Firebase Storage-How to delete file from storage with node.js?

别说谁变了你拦得住时间么 提交于 2019-12-14 03:08:43

问题


I want to delete a folder in firebase storage with node js because this is a firebase function.

For example :

storageRef.child(child1).child(child2).delete();

something like this, but firebase documentation doesn't tell anything.

One more question: When initialize storage documentation node js requires my admin json, but realtime database doesn't want this wonder why?


回答1:


Have a look at the Node.js client API Reference for Google Cloud Storage and in particular at the delete() method for a File.




回答2:


This might be late but at least on Web (so basically what you need), there is new API to delete the whole folder.

I tested deleting a folder with 2 pictures inside and it works. I then tried a folder-A with contents: folder-B + picture-A. Folder-B also has a picture-B inside; it still deleted folder-A with all of its contents.

Solution:

const bucket = admin.storage().bucket();

return bucket.deleteFiles({
  prefix: `posts/${postId}`
);

I couldn't find this on the official documentation (perhaps is really new API) but really cool article where I found the solution: Automatically delete your Firebase Storage Files from Firestore with Cloud Functions for Firebase



来源:https://stackoverflow.com/questions/54089533/firebase-storage-how-to-delete-file-from-storage-with-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!