简体   繁体   English

如何从 Firebase 云 Function 中删除 Firebase 存储文件夹?

[英]How to delete a Firebase Storage folder from a Firebase Cloud Function?

I couldn't find the deleteFiles() method in the Firebase API reference.我在 Firebase API 参考中找不到deleteFiles()方法。 My IDE tells me this method takes an optional DeleteFilesOptions argument and I couldn't find any information on that type as well.我的 IDE 告诉我这个方法有一个可选的DeleteFilesOptions参数,我也找不到关于该类型的任何信息。 If someone could point me to this documentation I would appreciate it.如果有人可以向我指出此文档,我将不胜感激。

That said, I've seen a number of posts that use this method, with this argument, to delete an entire Storage folder (and all of its files) through a Cloud Function. My question is, is this the correct way to do it (since the documentation here is missing)?也就是说,我已经看到许多帖子使用此方法和此参数通过 Cloud Function 删除整个存储文件夹(及其所有文件)。我的问题是,这是正确的方法吗(因为缺少此处的文档)?

const functions = require("firebase-functions");
const admin = require("firebase-admin");

exports.deleteStorageFolder = functions.https.onCall(async (data, _context) => {
    const uid = data.userId;

    try {
        const bucket = admin.storage().bucket(); // returns the default bucket, which is good
        await bucket.deleteFiles({
            prefix: `images/users/${uid}`, // the path of the folder
        });
        return Promise.resolve(true);
    } catch (error) {
        throw new functions.https.HttpsError("unknown", "Failed to delete storage folder.", error);
    }
});

As @Doug already mentioned in the comment, "Firebase just provides wrappers around Cloud Storage. They are the same thing.".正如@Doug 在评论中已经提到的那样,“Firebase 只是提供了云存储的包装器。它们是同一回事。”。 Also, according to this documentation , "Cloud Storage for Firebase stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud. This allows you the flexibility to upload and download files from mobile clients via the Firebase SDKs for Cloud Storage."此外,根据此文档,“Firebase 的云存储将您的文件存储在谷歌云存储桶中,使它们可以通过 Firebase 和谷歌云访问。这使您可以灵活地通过 Firebase SDK 从移动客户端上传和下载文件云储存。”

Having been said that, I've tried replicating the code snippet you've provided using deleteFiles() , and it worked fine on my end:话虽如此,我已经尝试使用deleteFiles()复制您提供的代码片段,并且它在我这边运行良好:

// // The Firebase Admin SDK to access Firestore.
const functions = require("firebase-functions");
const admin = require('firebase-admin');

const firebaseConfig = {
    // Your Firebase configuration...
};

admin.initializeApp(firebaseConfig);

const bucket = admin.storage().bucket();
async function deleteFolder(){
    await bucket.deleteFiles({
    prefix: 'images/users/${uid}'   // the path of the folder
    });
}

deleteFolder();

One another option that you can do is to directly use Google Cloud Storage, and skip using the Firebase Storage:您可以做的另一种选择是直接使用 Google Cloud Storage,并跳过使用 Firebase 存储:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket("your-bucket-name");

bucket.deleteFiles({
  prefix: 'images/users/${uid}'
}, function(err) {
  if (!err) {
    console.log("All files in the `images` directory have been deleted.");    
  }
});

Just a note, following the suggestion of Doug, you can try and test it out first in your local or test environment.请注意,按照 Doug 的建议,您可以先在本地或测试环境中尝试并测试它。 For further reference, you can refer todelete() and deleteFiles()如需进一步参考,您可以参考delete()deleteFiles()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM