如何获取所有存储在Firebase存储文件夹中的图像? [英] How to get all images stored in firebase storage folder?

查看:59
本文介绍了如何获取所有存储在Firebase存储文件夹中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Xamarin.iOS应用程序中具有图像上传功能.我已将此上传的图片存储在Firebase存储中.我的文件已成功上传到Firebase存储,但是问题是:

当我尝试使用Firebase的listAll()方法获取所有图像时除非文件夹中的图片> = 2,否则它不会返回所有图片.

将图像上传到Firebase存储上的代码:

 私有无效ImagePicker_FinishedPickingMedia(对象发送者,UIImagePickerMediaPickedEventArgs e){如果(e.Info [UIImagePickerController.MediaType] .ToString()=="public.image"){NSData imgData =新的NSData();imgData = e.OriginalImage.AsJPEG();Guid uniqId = Guid.NewGuid();//使用-uniqId.ToString()FirebaseClient.Instance.UploadAdventurePhoto(imgData,this.Adventure.Id,uniqId.ToString());//(文件夹路径-gs://myapp.appspot.com/adventures/00ac45a3-7c92-4335-a4b8-b9b705c4dd72)StorageReference photsUrl = Storage.DefaultInstance.GetReferenceFromUrl($"gs://myapp.appspot.com/adventures/{this.Adventure.Id}");photsUrl.ListAll(this.Handler);}this.imagePicker.DismissViewController(true,null);}//将图像添加到Firestore集合的文档中.私有异步void处理程序(StorageListResult结果,NSError arg2){foreach(result.Items中的可变图像){//将图片附加到Firestore文档的逻辑.}} 


 ///< param name =" imgData">需要存储在存储器中的所选图像.///< param name ="adventureId">文件夹名称.</param>///< param name ="imageName">通过此名称图像将被存储在文件夹中.公共无效UploadAdventurePhoto(NSData imgData,字符串AdventureId,字符串imageName){StorageReference adventurePictureRef = Storage.DefaultInstance.GetReferenceFromPath($"adventures/{adventureId}/{imageName}");StorageMetadata metaData =新的StorageMetadata();metaData.ContentType ="image/jpg";adventurePictureRef.PutData(imgData,metaData);} 

上传第一张图片后,图片成功上传,但是当调用处理程序时,它会给出以下响应:

但是在此之后,当我上传第二张图像时,它给出了Firebase.Storage.StorageReference

表示如果有两个图像,则只有它返回url引用.如何解决此问题?

我已经在存储规则中添加了 rules_version ='2'; .

解决方案

还没有解决listAll()的方法,但是我已经解决了这个问题.

我正在尝试的是使用listAll()从Firebase存储中获取所有图像,但是在这种情况下,我遇到了这个问题.因此,现在我不是使用listAll()而是使用PutData()方法的完成处理程序.

完成处理程序将为您提供上载图像的元数据.从此元数据中,我们可以像这样直接获取图像: metadata.Name

这是我通过在UploadAdventurePhoto()方法中添加完成处理程序来解决此问题的方法:

 私有无效UploadAdventurePhoto(NSData imgData,字符串folderName,字符串imageName){StorageReference adventurePictureRef = Storage.DefaultInstance.GetReferenceFromPath($"adventures/{folderName}/{imageName}");StorageMetadata metaData =新的StorageMetadata();metaData.ContentType =图像/jpeg";AdventurePictureRef.PutData(imgData,metaData,this.HandleStorageGetPutUpdateCompletion);} 


 私有异步void HandleStorageGetPutUpdateCompletion(StorageMetadata元数据,NSError错误){如果(错误!= null){//哦,发生错误!返回;}var url = metadata.Name;var downloadUrl = metadata.Path;Debug.WriteLine($图片网址-{url} \ n路径-{downloadUrl}");CollectionReference collectionRef = Firestore.SharedInstance.GetCollection(FirebaseClient.AdventuresCollection);var docRef = collectionRef.GetDocument(this.Adventure.Id);var键=新的NSString []{新的NSString($" {AdventureBase.PhotoPropName}"),};var值=新的NSObject []{新的NSString(url),};var objects = new NSObject []{FieldValue.FromArrayUnion(value),};var dict = new NSDictionary< NSString,NSObject>(键,对象);等待docRef.SetDataAsync(dict,true);docRef.AddSnapshotListener(this.UpdateDataHandler);} 


 私有异步void UpdateDataHandler(DocumentSnapshot快照,NSError错误){如果(错误!= null){//出问题了Debug.WriteLine($错误-{error.Description}"));返回;}Toast.MakeToast(图片上传成功").Show();} 

I have image upload feature in my Xamarin.iOS application. I have stored this uploaded image(s) in firebase storage. My files gets uploaded to firebase storage successfully, but the issue is:

When I am trying to get all images using listAll() method of firebase storage it not return all images until the folder have images >= 2.

Code to upload image on firebase storage:

private void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
    if (e.Info[UIImagePickerController.MediaType].ToString() == "public.image")
    {
        NSData imgData = new NSData();
        imgData = e.OriginalImage.AsJPEG();

        Guid uniqId = Guid.NewGuid(); // use - uniqId.ToString()

        FirebaseClient.Instance.UploadAdventurePhoto(imgData, this.Adventure.Id, uniqId.ToString());

        //(Path of folder - gs://myapp.appspot.com/adventures/00ac45a3-7c92-4335-a4b8-b9b705c4dd72)
        StorageReference photsUrl = Storage.DefaultInstance.GetReferenceFromUrl($"gs://myapp.appspot.com/adventures/{this.Adventure.Id}");
        photsUrl.ListAll(this.Handler);
    }
    this.imagePicker.DismissViewController(true, null);
}

// Add image to Firestore collection's Document.
private async void Handler(StorageListResult result, NSError arg2)
{
    foreach (var image in result.Items)
    {
        // Logic to append image to Firestore document.
    }
}


/// <param name="imgData">Selected image that needs to be stored on storage.</param>
/// <param name="adventureId">Name of the folder.</param>
/// <param name="imageName">By this name image will get stored in folder.</param>
public void UploadAdventurePhoto(NSData imgData, string adventureId, string imageName)
{
    StorageReference adventurePictureRef = Storage.DefaultInstance.GetReferenceFromPath($"adventures/{adventureId}/{imageName}");
    StorageMetadata metaData = new StorageMetadata();
    metaData.ContentType = "image/jpg";
    adventurePictureRef.PutData(imgData, metaData);
}

After first image get uploaded, image get uploaded successfully but when handler gets called it gives this response:

But after this when I upload 2nd image that time it give Firebase.Storage.StorageReference1 in response:

Means if there are two images then only it returns url reference. How to fix this issue?

I have already added rules_version = '2'; in storage rules.

解决方案

Haven't got the solution for listAll() but I have got the work around for this problem.

What I was trying is to get all the images from firebase storage using listAll(), but in that I am getting this issue. So now instead of listAll() I am using PutData() method's completion handler.

The completion handler will provide you the Metadata of an uploaded image. From this meta data we can get image directly like this: metadata.Name

Here is how I have fix the problem by adding completion handler in UploadAdventurePhoto() method:

private void UploadAdventurePhoto(NSData imgData, string folderName, string imageName)
{
    StorageReference adventurePictureRef = Storage.DefaultInstance.GetReferenceFromPath($"adventures/{folderName}/{imageName}");
    StorageMetadata metaData = new StorageMetadata();
    metaData.ContentType = "image/jpeg";
    adventurePictureRef.PutData(imgData, metaData, this.HandleStorageGetPutUpdateCompletion);
}


private async void HandleStorageGetPutUpdateCompletion(StorageMetadata metadata, NSError error)
{
    if (error != null)
    {
        // Uh-oh, an error occurred!
        return;
    }

    var url = metadata.Name;
    var downloadUrl = metadata.Path;
    Debug.WriteLine($"Image url - {url}\n Path-{downloadUrl}");

    CollectionReference collectionRef = Firestore.SharedInstance.GetCollection(FirebaseClient.AdventuresCollection);
    var docRef = collectionRef.GetDocument(this.Adventure.Id);

    var keys = new NSString[]
    {
        new NSString($"{AdventureBase.PhotoPropName}"),
    };
    var value = new NSObject[]
    {
        new NSString(url),
    };
    var objects = new NSObject[]
    {
        FieldValue.FromArrayUnion(value),
    };

    var dict = new NSDictionary<NSString, NSObject>(keys, objects);

    await docRef.SetDataAsync(dict, true);
    docRef.AddSnapshotListener(this.UpdateDataHandler);
}


private async void UpdateDataHandler(DocumentSnapshot snapshot, NSError error)
{
    if (error != null)
    {
        // something went wrong
        Debug.WriteLine($"Error - {error.Description}");
        return;
    }
    Toast.MakeToast("Image uploaded successfully").Show();
}

这篇关于如何获取所有存储在Firebase存储文件夹中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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