如何使用颤振删除 Firebase 存储文件? [英] How to delete a Firebase Storage file with flutter?

查看:21
本文介绍了如何使用颤振删除 Firebase 存储文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Flutter 和 Firebase 都不熟悉,所以请耐心等待.

I'm new to both Flutter and Firebase, so bear with me on this one.

我正在尝试使用文件 Url 删除 Firebase 存储中的文件.

I am trying to delete a File in my Firebase Storage using the file Url.

我有文件的完整 Url,并删除了文件路径之外的所有字符.

I have the full Url of the file and remove all characters excluding the file path.

然后我尝试删除该文件.从这里的其他答案开始,我使用命令;FirebaseStorage.instance.ref().child(filePath).delete()

Then I try to delete the file. From following other answers on here I use the command; FirebaseStorage.instance.ref().child(filePath).delete()

下面是我的代码;

static void deleteFireBaseStorageItem(String fileUrl){

  String filePath = 'https://firebasestorage.googleapis.com/v0/b/dial-i-2345.appspot.com/o/default_images%2Fuser.png?alt=media&token=c2ccceec-8d24-42fe-b5c0-c987733ac8ae'
                  .replaceAll(new 
                  RegExp(r'https://firebasestorage.googleapis.com/v0/b/dial-in-2345.appspot.com/o/'), '');

  FirebaseStorage.instance.ref().child(filePath).delete().then((_) => print('Successfully deleted $filePath storage item' ));

}

问题是文件永远不会被删除.

The problem is the file never gets deleted.

我想我已经找到了问题所在.

I think I have tracked down where the problem is.

.delete() 方法中有一个必需的app"和storageBucket"值.

In the .delete() method there is a required 'app' and 'storageBucket' value.

当我运行这个函数时,值为空.

When I run this function the values are null.

我觉得这很令人困惑,因为我在同一个文件中看到了这个;

I find this confusing because in the same file i see this;

/// The [FirebaseApp] instance to which this [FirebaseStorage] belongs.
///
/// If null, the default [FirebaseApp] is used.
final FirebaseApp app;

/// The Google Cloud Storage bucket to which this [FirebaseStorage] belongs.
///
/// If null, the storage bucket of the specified [FirebaseApp] is used.
final String storageBucket;enter code here

从文档中我可以看到,我可能需要使用Firebase core"插件来制作将存储桶链接到该数据库的应用程序.但是对于 Dart & 并没有太多特定的内容.颤抖.

From the documentation I can see maybe I need to use the 'Firebase core' plugin to make the app which links the bucket to that database. But there is not much specific to Dart & flutter.

有人有这方面的经验吗?

Does anyone have any experience with this?

所以暂时没有将Url转换为存储引用的方法.希望它会在 Firebase 更新中发布.

So there is not a method to convert the Url to a storage reference at the moment. Hopefully it will come out in a Firebase update.

这是我的黑客

 static void deleteFireBaseStorageItem(String fileUrl){

String filePath = fileUrl
                  .replaceAll(new 
                  RegExp(r'https://firebasestorage.googleapis.com/v0/b/dial-in-2345.appspot.com/o/'), '');

filePath = filePath.replaceAll(new RegExp(r'%2F'), '/');

filePath = filePath.replaceAll(new RegExp(r'(?alt).*'), '');

StorageReference storageReferance = FirebaseStorage.instance.ref();

storageReferance.child(filePath).delete().then((_) => print('Successfully deleted $filePath storage item' ));

}

我知道……我知道……

Ps 'dial-in-2345.appspot.com' 与我的应用相关,您需要更改它.

Ps 'dial-in-2345.appspot.com' is relevant to my app and you would need to change it.

推荐答案

更新(2021-05-22):

Update (2021-05-22):

FirebaseStorage.instance.refFromURL(url).delete().之前建议的 getReferenceFromUrl 似乎从 API 中消失了.

FirebaseStorage.instance.refFromURL(url).delete() should be used now. getReferenceFromUrl that was suggested earlier seems to be gone from the API.

更新(2019-08-03):

Update (2019-08-03):

根据 这个 PR 现在有一个 getReferenceFromUrl 方法,允许通过下载查找存储引用网址.

According to this PR there is now a getReferenceFromUrl method in FlutterFire that allows looking up a storage reference by its download URL.

上一个答案:

在 Android 上,您可以调用 getReferenceForUrl() 从下载 URL 获取 StorageReference,iOS 上也有类似的方法.

On Android you can call getReferenceForUrl() to get a StorageReference from a download URL, and a similar method exists on iOS.

但是我在FlutterFire 中找不到对应的方法参考文档.不幸的是,这意味着在 Flutter 中无法从下载 URL 映射回 StorageReference.

But I can't find a corresponding method in the FlutterFire reference docs. This unfortunately means that there is no way to map from a download URL back to a StorageReference in Flutter.

这听起来像是一个遗漏,所以我建议在这个功能请求上投你的+1.

This sounds like an omission, so I recommend casting your +1 on this feature request.

目前这意味着您需要在 Cloud Storage 中拥有文件的相对路径才能从 Flutter 中删除它.使用该路径,您当前的代码将起作用.

For the moment this means you'll need to have the relative path to the file in Cloud Storage to be able to delete it from Flutter. With that path, your current code would work.

这篇关于如何使用颤振删除 Firebase 存储文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆