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

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

问题描述

我对Flutter和Firebase还是陌生的,所以请耐心等待。


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

p>

我拥有文件的完整网址,并删除文件路径以外的所有字符。


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


下面是我的代码;

 静态无效deleteFireBaseStorageItem(字符串fileUrl){

字符串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('成功删除$ filePath存储项目'));

}

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


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


.delete()方法中, app和 storageBucket值。


运行此函数时,该值为空。



我发现这个令人困惑,因为在同一文件中我看到了这一点;

  ///此[FirebaseStorage]所属的[FirebaseApp]实例。 
///
///如果为null,则使用默认值[FirebaseApp]。
最终的FirebaseApp应用;

/// [FirebaseStorage]所属的Google Cloud Storage存储桶。
///
///如果为null,则使用指定[FirebaseApp]的存储桶。
最终字符串storageBucket;在此处输入代码

从文档中我可以看到也许我需要使用 Firebase核心插件来制作将存储桶链接到该数据库的应用。但是Dart& amp;并没有太多具体


有人对此有任何经验吗?


找到了临时解决方案


所以没有此刻将Url转换为存储引用的方法。希望它将在Firebase更新中发布。


这是我的hack

 静态无效deleteFireBaseStorageItem(String fileUrl){

字符串filePath = fileUrl
.replaceAll(新
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('成功删除$ filePath存储项'));

}


我知道...。我知道....


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

解决方案

更新(2019-08-03):



根据此PR 现在有一个 getReferenceFromUrl 方法(可通过其下载URL查找存储引用)。






上一个答案:



在Android上,您可以致电 getReferenceForUrl() 从下载URL获取 StorageReference ,以及类似的方法exis ts在iOS上。



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



这听起来像是一个遗漏,所以我建议在此功能请求



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


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

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

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

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

Below is my code;

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.

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

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?

Found Temporary solution

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.

Here was my hack

 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' ));

}

I know.... i know....

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

解决方案

Update (2019-08-03):

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


Previous answer:

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

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.

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

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.

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

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