B4A Library FirebaseStorage - Simple file storage backend

Status
Not open for further replies.
FirebaseStorage service is similar to a FTP server. Clients can upload and download files.
FirebaseStorage takes it a step further and adds an authorization layer.

Google are offering a free package and a paid package: https://firebase.google.com/pricing/
The free offer is quite generous.

FirebaseStorage works together with FirebaseAuth for the access control.

The rules are set in Firebase console. I recommend to start with these rules (make sure to update the service in the second line based on your app id):
B4X:
service firebase.storage {
  match /b/b4a-test1.appspot.com/o {
  match /auth/{allPaths=**} {
     allow read, write: if request.auth != null;
   }
  match /public/{allPaths=**} {
  allow read;
  }
  match /user/{userId}/{allPaths=**} {
  allow read, write: if request.auth.uid == userId;
  }

  }
}

With these rules there are three accessible folders with the following access levels:
/public - Anyone can read from this folder. You can upload files to this folder from the console. This is a good place for any general files (images, data sets). Note that these files can be accessed from outside your app.
/auth - All authenticated users can read and write to this folder. Resources limited to the app users.
/user/{userId} - Only the user can access this folder. User's private resources.
Subfolders will have the same access control as their parent folders.

Setup instructions

Follow the Firebase integration instructions: https://www.b4x.com/android/forum/threads/integrating-firebase-services.67692/#content
Add the Auth snippet as well as the base snippets.

The code is simple. You need to initialize FirebaseStorage, preferably from the Starter service, with your bucket url. You can find it in Firebase console (gs://...):

SS-2016-06-26_14.56.07.png


You can now upload and download files and also get the available metadata.
All the methods are asynchronous which means that an event is raised when the operation completes. The events will be raised in the same module that started the operation.

Note that you can use FirebaseStorage without FirebaseAuth and then use it to download files from the public folder.
 

Attachments

  • StorageExample.zip
    24.7 KB · Views: 1,290
Last edited:

johndb

Active Member
Licensed User
Longtime User
I do not want to seem insistent, but how?
You cannot create top level folders within the app. These must be created in the FB dashboard as they require permissions to be set.
Folders below the defined top level folders will automatically be created when you upload files from the app and have a path defined.
 

Alpandino

Member
Licensed User
You cannot create top level folders within the app. These must be created in the FB dashboard as they require permissions to be set.
Folders below the defined top level folders will automatically be created when you upload files from the app and have a path defined.
I can confirm this. In my app I'using this library and it works perfectly creating the needed folder if it doesn't exist. Anyway you can't delete folders. But this is a false problem.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Fyi - call me stupid, but I struggled with this for quite a while before I realized that the "app id" that Erel referred to the rules is actually the Firebase Storage bucket ID, NOT the package name... DOH! It's probably spelled out very clearly somewhere, but I guess I missed it...

- Colin.
 

Claudio Oliveira

Active Member
Licensed User
Longtime User
I'm playing around with this lib and I just don't get to put it to work.
I can initialize the FirebaseStorage class successfully, but every single method called ends in error.
I'm trying to run this simple code:
B4X:
Starter.STORAGE.UploadFile(File.DirRootExternal,"text.txt",$"/public/test.txt"$)
And all I get is this:
B4X:
(IOException) java.io.IOException: The server has terminated the upload session

I've already checked everything out a zillion times, and everything is absolutely fine.
But I still don't get it working...

Any help would be highly appreciated!

By the way: The class name in the library is FilrebaseStorage :confused:

Regards

EDIT: SOLVED

I've changed the 2nd line of the rules from
B4X:
match /b/b4a-test1.appspot.com/o {
to
B4X:
match /b/{bucket}/o  {
and everything's OK now.

But it'd be a good idea fixing the class name on the lib's XML. I did that here. ;)

Regards
 
Last edited:

zolive33

Active Member
Licensed User
Longtime User
Hello, is it possible to display download (or upload) progression?

Regards.
 

JEG

Member
Licensed User
Longtime User
Compiling either of the downloaded files in post 1 the following error arises. Where can the Json file be found?

upload_2018-6-30_9-55-51.png
 
Can you add timeout function, because some times users can not connect to Internet, we need check it because events completed will never fire without internet!!!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Always better to start a new thread for your questions.

Monitoring downloads: https://www.b4x.com/android/forum/t...load-or-upload-progression.86654/#post-548584

You can implement your own timeout mechanism with something like:
B4X:
Sub btnDownloadPublic_Click
    Dim path As String = "/user/test"
    Starter.storage.DownloadFile(path, File.DirInternal, "test")
    DownloadingPaths.Remove(path)
    Dim start As Long = DateTime.Now
    Do While DownloadingPaths.ContainsKey(path) = False Or DateTime.Now > start + 30 * DateTime.TicksPerSecond
        Sleep(500)
    Loop
    If DownloadingPaths.ContainsKey(path) = False Then
        Log("timeout reached")
    Else if False = DownloadingPaths.Get(path) Then
        Log("failed to download")
    Else
        Log("success")
    End If
End Sub


Sub Storage_DownloadCompleted (ServerPath As String, Success As Boolean)
    DownloadingPaths.Put(ServerPath, Success)
End Sub
DownloadingPaths is a global Map.
 
Status
Not open for further replies.
Top