【问题标题】:How to delete file from Firebase Storage in Flutter如何在 Flutter 中从 Firebase 存储中删除文件
【发布时间】:2020-05-10 10:58:34
【问题描述】:

我尝试了这篇文章中的示例How to delete a Firebase Storage file with flutter?

但它没有工作。这是我尝试过的:

String filePath = 'https://firebasestorage.googleapis.com/v0/b/amct-47348.appspot.com/o/ClassificadoImages%2F1579839318515.jpg?alt=media&token=cd8880eb-8b37-45e4-8dc1-e75de0c5f7cb'
  .replaceAll(new RegExp(r 'https://firebasestorage.googleapis.com/v0/b/amct-47348.appspot.com/o/ClassificadoImages%2F'), '').split('?')[0];

FirebaseStorage.instance.ref().child(filePath).delete()
  .then((_) {
    print('Successfully deleted $filePath storage item');
  }).catchError((e) {
    print("err: $e");
  });

这是它返回的内容:

I / flutter(4920): err: PlatformException(deletion_error, Object does not exist at location., null)

可以对它返回错误的原因提出一些想法,因为它与示例完全相同。感谢阅读。

【问题讨论】:

    标签: flutter google-cloud-storage firebase-storage


    【解决方案1】:

    Firebase SDK 采用文件在存储分区中的路径。他们不采用 URL 或拆分 URL。它们也不采用 url 编码的字符串。路径必须是用于上传它们的纯字符串。

    只是猜测,但如果您有以

    开头的下载 URL

    https://firebasestorage.googleapis.com/v0/b/amct-47348.appspot.com/o/ClassificadoImages%2F1579839318515.jpg"

    那么文件的字符串路径大概是

    ClassificadoImages/1579839318515.jpg

    这就是您应该传递给FirebaseStorage.instance.ref().child() 以构建使用它的引用(因为 %2F 是 URL 中正斜杠的编码版本)。

    【讨论】:

    • 感谢您的意见。只是遵循示例“hack”,因为在 Flutter 中似乎还没有正式的方法来执行此操作。问题是从下载 url 获取存储桶路径的引用。
    【解决方案2】:

    如果您有存储在 firesbase 存储中的图像的 url,您可以使用

    **StorageReference ** 中的

    getReferenceFromUrl 方法。

    然后我们可以使用引用删除它。

    FirebaseStorage.instance
        .getReferenceFromUrl(imageData)
        .then((reference) => reference.delete())
        .catchError((e) => print(e));
    

    【讨论】:

    • getReferenceFromUrl 无效
    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 2019-12-17
    • 2022-01-02
    • 2021-07-07
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多