-
Notifications
You must be signed in to change notification settings - Fork 35
/
Program.cs
64 lines (54 loc) · 2.22 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace Firebase.Storage.SimpleConsole
{
using Firebase.Auth;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
internal class Program
{
private static string ApiKey = "YOUR_API_KEY";
private static string Bucket = "your-bucket.appspot.com";
private static string AuthEmail = "your@email.com";
private static string AuthPassword = "yourpasword";
private static void Main(string[] args)
{
Run().Wait();
}
private static async Task Run()
{
// FirebaseStorage.Put method accepts any type of stream.
var stream = new MemoryStream(Encoding.ASCII.GetBytes("Hello world!"));
//var stream = File.Open(@"C:\someFile.png", FileMode.Open);
// of course you can login using other method, not just email+password
var auth = new FirebaseAuthProvider(new FirebaseConfig(ApiKey));
var a = await auth.SignInWithEmailAndPasswordAsync(AuthEmail, AuthPassword);
// you can use CancellationTokenSource to cancel the upload midway
var cancellation = new CancellationTokenSource();
var task = new FirebaseStorage(
Bucket,
new FirebaseStorageOptions
{
AuthTokenAsyncFactory = () => Task.FromResult(a.FirebaseToken),
ThrowOnCancel = true // when you cancel the upload, exception is thrown. By default no exception is thrown
})
.Child("receipts")
.Child("test")
.Child("someFile.png")
.PutAsync(stream, cancellation.Token);
task.Progress.ProgressChanged += (s, e) => Console.WriteLine($"Progress: {e.Percentage} %");
// cancel the upload
// cancellation.Cancel();
try
{
// error during upload will be thrown when you await the task
Console.WriteLine("Download link:\n" + await task);
}
catch (Exception ex)
{
Console.WriteLine("Exception was thrown: {0}", ex);
}
}
}
}