简体   繁体   English

firebase 函数中有没有办法删除与正则表达式匹配的存储文件?

[英]Is there a way in firebase functions to delete storage files that match a regular expression?

I can't find a way to delete all the files inside firebase storage that match a regular expression.我找不到删除 Firebase 存储中与正则表达式匹配的所有文件的方法。 I would like to use something like:我想使用类似的东西:

        const bucket = storage.bucket(functions.config().firebase.storageBucket);
        const filePath = 'images/*.png';
        const file = bucket.file(filePath);
        file.delete();

Or similar to be able to delete all files inside "images" with png extension.或类似能够删除“图像”中带有 png 扩展名的所有文件。

I tried searching in Stackoverflow and in the samples repository without luck https://github.com/firebase/functions-samples我尝试在 Stackoverflow 和示例存储库中搜索,但没有运气https://github.com/firebase/functions-samples

Any help would be much appreciated.任何帮助将非常感激。 Thanks!谢谢!

No. In order to delete a file from Cloud Storage, you need to be able to construct a full path to that file.不可以。要从 Cloud Storage 中删除文件,您需要能够构建该文件的完整路径。 There are no wildcards or regular expressions.没有通配符或正则表达式。

It's common to store the paths to files in a database in order to easily discover the names of files to delete by using some query.通常将文件的路径存储在数据库中,以便通过使用某些查询轻松发现要删除的文件的名称。

If you want to do this in Cloud Functions, you'll be using the Firebase Admin SDK.如果您想在 Cloud Functions 中执行此操作,您将使用 Firebase Admin SDK。 And the Cloud Storage functionality in there ( admin.storage() ) is a thin wrapper around the Cloud Storage SDK for Node.js.其中的云存储功能 ( admin.storage() ) 是针对 Node.js 的云存储 SDK 的精简包装。 So if you search for cloud storage node.js path regular expression you'll get some relevant results.所以如果你搜索cloud storage node.js path regular expression你会得到一些相关的结果。

What about this solution这个解决方案怎么样

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference mStorageRef = 
    storage.getReference().child("fotos_usuarios/"+id);
mStorageRef.listAll().addOnSuccessListener(listResult -> {
    for (StorageReference item : listResult.getItems()) {
        if (item.getName().contains("SOME_WORD")){
            item.delete();
        }
    }
})

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

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