简体   繁体   English

从 Flutter 中的 Firebase 云存储中删除文件夹

[英]Deleting a folder from Firebase Cloud Storage in Flutter

I have a Flutter mobile app in which I am trying to delete a folder (and its contents) from Firebase Cloud Storage.我有一个 Flutter 移动应用程序,我正在尝试从 Firebase 云存储中删除一个文件夹(及其内容)。 My method is as follows:我的方法如下:

deleteFromFirebaseStorage() async {
     return await FirebaseStorage.instance.ref().child('Parent folder/Child folder').delete();
}

I expect Child folder and its contents to be deleted, but this exception is thrown:我希望删除Child folder及其内容,但会引发此异常:

Unhandled Exception: PlatformException(Error -13010, FIRStorageErrorDomain, Object Parent folder/Child folder does not exist.)未处理的异常:PlatformException(错误 -13010,FIRStorageErrorDomain,Object 父文件夹/子文件夹不存在。)

However I can clearly see that folder exists in Cloud Storage.但是我可以清楚地看到该文件夹存在于 Cloud Storage 中。 How do I delete this folder?如何删除此文件夹?

Cloud Storage doesn't actually have any folders. Cloud Storage 实际上没有任何文件夹。 There are just paths that look like folders, to help you think about how your structure your data.只有看起来像文件夹的路径,以帮助您考虑如何构建数据。 Each object can just have a common prefix that describes its "virtual location" in the bucket.每个 object 只能有一个通用前缀来描述其在存储桶中的“虚拟位置”。

There's no operations exposed by the Firebase SDKs that make it easy to delete all objects in one of these common prefixes. Firebase SDK 没有公开任何操作,可以轻松删除这些常用前缀之一中的所有对象。 Your only real option is to list all files in a common prefix, iterate the results, and delete each object individually.您唯一真正的选择是以公共前缀列出所有文件,迭代结果,然后单独删除每个 object。

Unfortunately, the list files API has not made it to flutter yet, as discussed here .不幸的是,列表文件 API还没有进入 flutter,如此所述。 So you are kind of out of luck as far as an easy solution is concerned.因此,就简单的解决方案而言,您有点不走运。 See also: FirebaseStorage: How to Delete Directory另请参阅: FirebaseStorage:如何删除目录

Your primary viable options are:您的主要可行选择是:

MD.医学博士。 Saffan Alvy was right, but to completely delete all and not just one file do this. Saffan Alvy 是对的,但是要完全删除所有文件而不是一个文件,请执行此操作。 if you didn't know.如果你不知道。

await await FirebaseStorage.instance.ref("users/${FirebaseAuth.instance.currentUser!.uid}/media").listAll().then((value) {
value.items.forEach((element) {
FirebaseStorage.instance.ref(element.fullPath).delete();
);
});

Currently, I'm working on a project that contains a single file inside every folder so I did this and it worked for me.目前,我正在开发一个项目,该项目在每个文件夹中都包含一个文件,所以我这样做了,它对我有用。

await FirebaseStorage.instance.ref("path/" + to + "/" + folder)
                  .listAll().then((value) {
FirebaseStorage.instance.ref(value.items.first.fullPath).delete();
});

This code deletes the file and folder as well.此代码也会删除文件和文件夹。 Since, there's no folder here, deleting the file deletes the folder or reference.由于此处没有文件夹,因此删除文件会删除文件夹或引用。 You can use foreach loop or map to delete everything from the folder if you have multiple files.如果您有多个文件,您可以使用 foreach 循环或 map 从文件夹中删除所有内容。

I built a script that deletes recursively all files inside a folder and all subfolders, here is:我构建了一个脚本,它递归地删除文件夹和所有子文件夹中的所有文件,这里是:

import 'package:firebase_storage/firebase_storage.dart';

class FirebaseStorageApi {

  static Future<void> deleteFolder({
    required String path
  }) async {
    List<String> paths = [];
    paths = await _deleteFolder(path, paths);
    for (String path in paths) {
      await FirebaseStorage.instance.ref().child(path).delete();
    }
  }

  static Future<List<String>> _deleteFolder(String folder, List<String> paths) async {
    ListResult list = await FirebaseStorage.instance.ref().child(folder).listAll();
    List<Reference> items = list.items;
    List<Reference> prefixes = list.prefixes;
    for (Reference item in items) {
      paths.add(item.fullPath);
    }
    for (Reference subfolder in prefixes) {
      paths = await _deleteFolder(subfolder.fullPath, paths);
    }
    return paths;
  }

}

Usage:用法:

await FirebaseStorageApi.deleteFolder(path: "YOUR/FOLDER/PATH");

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

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