Skip to content

f-miyu/Plugin.FirebaseStorage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plugin.FirebaseStorage

A cross platform plugin for Firebase Storage. A wrapper for Xamarin.Firebase.iOS.Storage and Xamarin.Firebase.Storage.

Setup

Install Nuget package to each projects.

Plugin.FirebaseStorage NuGet

iOS

  • Add GoogleService-Info.plist to iOS project. Select BundleResource as build action.
  • Initialize as follows in AppDelegate.
Firebase.Core.App.Configure();

Android

  • Add google-services.json to Android project. Select GoogleServicesJson as build action. (If you can't select GoogleServicesJson, reload this android project.)
  • Target framework version needs to be Android 10.0.
  • Add the following into AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Usage

Update from bytes

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

await reference.PutBytesAsync(bytes);

Update from a stream

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

await reference.PutStreamAsync(stream);

Update from a local file

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

await reference.PutFileAsync(filePath);

Upload with metadata

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var metadata = new MetadataChange
{
    ContentType = "image/jpeg"
};

await reference.PutStreamAsync(stream, metadata);

Monitor upload progress

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var uploadProgress = new Progress<IUploadState>();
uploadProgress.ProgressChanged += (sender, e) =>
{
    var progress = e.TotalByteCount > 0 ? 100.0 * e.BytesTransferred / e.TotalByteCount : 0;
};

await reference.PutStreamAsync(stream, progress: uploadProgress);

Cancel upload

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var cts = new CancellationTokenSource();
var task = Task.Run(async () =>
{
     await Task.Delay(1000);
     
     cts.Cancel();
});

await reference.PutStreamAsync(stream, cancellationToken: cts.Token);

Pause upload

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var pts = new PauseTokenSource();
var task = Task.Run(async () =>
{
     await Task.Delay(1000);
     
     pts.Pause();
     
     await Task.Delay(1000);
     
     pts.Resume();
});

await reference.PutStreamAsync(stream, pauseToken: pts.Token);

Download to bytes

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var maxDownloadSizeBytes = 1024 * 1024;

var bytes = await reference.GetBytesAsync(maxDownloadSizeBytes);

Download to a stream

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var stream = await reference.GetStreamAsync();

Download to a local file

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

await reference.GetFileAsync(filePath);

Monitor download progress

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var downloadProgress = new Progress<IDownloadState>();
downloadProgress.ProgressChanged += (sender, e) =>
{
    var progress = e.TotalByteCount > 0 ? 100.0 * e.BytesTransferred / e.TotalByteCount : 0;
};

var stream = await reference.GetStreamAsync(downloadProgress);

Cancel download

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var cts = new CancellationTokenSource();
var task = Task.Run(async () =>
{
     await Task.Delay(1000);
     
     cts.Cancel();
});

var stream = await reference.GetStreamAsync(cancellationToken: cts.Token);

Get a download URL

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var url = await reference.GetDownloadUrlAsync();

Get metadata

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var metadata = await reference.GetMetadataAsync();

Update metadata

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

var metadata = new MetadataChange
{
    ContentType = "image/jpeg",
    CustomMetadata = new Dictionary<string, string>
    {
        ["myCustomProperty"] = "myValue"
    }
};

await reference.UpdateMetadataAsync(metadata);

Delete a file

var reference = CrossFirebaseStorage.Current.Instance.RootReference.Child("image.jpg");

await reference.DeleteAsync();

List files

// All
var result = await CrossFirebaseStorage.Current.Instance.RootReference.ListAllAsync();
var items = result.Items.ToList();

// Pagination
var result1 = await CrossFirebaseStorage.Current.Instance.RootReference.ListAsync(10);
var token = result1.PageToken;
var result2 = await CrossFirebaseStorage.Current.Instance.RootReference.ListAsync(10, token);

Use multiple projects

var reference = CrossFirebaseStorage.Current.GetInstance("SecondAppName").RootReference.Child("image.jpg");

await reference.PutBytesAsync(bytes);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages