B4A Library FirebaseStorage - Simple file storage backend

Erel

Administrator
Staff member
Licensed User
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://...):



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.

Updates

Example updated. The manifest code as updated with new required code (this is not required if using FirebaseAnalytics v1.10+):
B4X:
AddApplicationText(
 <service android:name="com.google.firebase.components.ComponentDiscoveryService" >
            <meta-data
                android:name="com.google.firebase.components:com.google.firebase.analytics.connector.internal.AnalyticsConnectorRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
            <meta-data
                android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
             <meta-data
                android:name="com.google.firebase.components:com.google.firebase.auth.FirebaseAuthRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
            <meta-data
                android:name="com.google.firebase.components:com.google.firebase.storage.StorageRegistrar"
                android:value="com.google.firebase.components.ComponentRegistrar" />
        </service>
)
 

Attachments

Last edited:

Erel

Administrator
Staff member
Licensed 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
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
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
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.
 

Erel

Administrator
Staff member
Licensed User
You are using B4A-Bridge, right? Try to uninstall the existing app.
 

freedom2000

Well-Known Member
Licensed 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
 

Erel

Administrator
Staff member
Licensed 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
 

freedom2000

Well-Known Member
Licensed 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
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?
 

Filippo

Expert
Licensed User
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?
 

Erel

Administrator
Staff member
Licensed User
They are not exactly the same. If you are looking for a way to store data in the cloud that is only accessible for your app then FirebaseStorage is most probably the best service.
 
Top