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,270
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
FirebaseStorage v1.05 was uploaded to the first post. This update changes the signature of UploadFile and DownloadFile methods. They now expect a file (folder and file name) instead of a stream. UploadStream and DownloadStream were added with the signatures of the previous methods.

The example was also updated.
 

karld

Active Member
Licensed User
Longtime User
I downloaded the FireBaseExample.zip file. When I load the code it tells me I am missing the FireBase libraries.
My version of B4A is 6.
Are the libraries available for download?
 

karld

Active Member
Licensed User
Longtime User
I downloaded the FireBaseExample.zip file. When I load the code it tells me I am missing the FireBase libraries.
My version of B4A is 6.
Are the libraries available for download?

Never mind. I found them (DUH!)

I need to sleep...
 

karld

Active Member
Licensed User
Longtime User
I am trying to use just simple storage without auth.
The app compiles without complaining. When I bridge it to the phone it asked if I wanted to install.
I said YES, and then it pops up and says app not installed.

I am using the example Eriel provided above.
I commented out the entries for Auth in the manifest, the starter, and in main.

Open to suggestions.
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

Nice library, which works great :)

However, would it be easy to handle the file delete API ?

B4X:
// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("gs://");

// Create a reference to the file to delete
StorageReference desertRef = storageRef.child("images/desert.jpg");

// Delete the file
desertRef.delete().addOnSuccessListener(new OnSuccessListener() {
    @Override
    public void onSuccess(Void aVoid) {
        // File deleted successfully
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred!
    }
});

Thanks
 

freedom2000

Well-Known Member
Licensed User
Longtime User
V1.10 was uploaded to the first post. It allows deleting remote resources.

B4X:
Starter.storage.DeleteFile($"/user/${Starter.auth.CurrentUser.Uid}/1.txt"$)

Private Sub Storage_DeleteCompleted (ServerPath As String, Success As Boolean)
   Log(Success)
End Sub
Thanks a lot @Erel :)
 

georgelbs

Member
Licensed User
Longtime User
Hi, is possible to automatize the upload of files to Firebase storage from a desktop program (B4J) and later download with B4A App?
May be an example?
 

freedom2000

Well-Known Member
Licensed User
Longtime User
And don't forget to add the delete flag into the rules :

B4X:
match /public/{allPaths=**}
  {
      allow delete;
  }

of course with better limits than this...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
And don't forget to add the delete flag into the rules :
It is not needed if writing is allowed.

Hi, is possible to automatize the upload of files to Firebase storage from a desktop program (B4J) and later download with B4A App?
B4J doesn't support FirebaseStorage.
 
D

Deleted member 103

Guest
One question, what would be better "FirebaseStorage" or the library "libGoogleDrive" to backup data?
Or better said, is with "FirebaseStorage" the library "libGoogleDrive" no longer necessary?
 
Status
Not open for further replies.
Top