B4A Library FirebaseStorage - Simple file storage backend

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jun 26, 2016.

  1. Erel

    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):
    Code:
    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://...):

    [​IMG]

    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.
     

    Attached Files:

  2. incendio

    incendio Active Member Licensed User

    Great, will there a library for Firebase realtime database?
     
  3. DonManfred

    DonManfred Expert Licensed User

    This is the WRONG thread for this question!
    Create a thread in the wishlist or the questionsforum.
     
    Jaames and Erel like this.
  4. asales

    asales Active Member Licensed User

    Did you know if this plan is per user, per app or per account that use firebase?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Based on my understanding it is per account.
     
    asales likes this.
  6. Erel

    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.
     
    luke2012 and johndb like this.
  7. karld

    karld 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?
     
  8. DonManfred

    DonManfred Expert Licensed User

    You find the lib at Post #1

    If that is not the one you think: Post the exact error you get

    Make sure you followed the Integrate Firebase tutorial
     
  9. karld

    karld Member Licensed User

    Never mind. I found them (DUH!)

    I need to sleep...
     
  10. karld

    karld 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.
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    You are using B4A-Bridge, right? Try to uninstall the existing app.
     
  12. karld

    karld Member Licensed User

    I will when I get home. Thanks
     
  13. freedom2000

    freedom2000 Well-Known Member Licensed User

    Hi,

    Nice library, which works great :)

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

    Code:
    // 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
     
    Daniel-White and johndb like this.
  14. Erel

    Erel Administrator Staff Member Licensed User

    V1.10 was uploaded to the first post. It allows deleting remote resources.

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

    Private Sub Storage_DeleteCompleted (ServerPath As String, Success As Boolean)
       
    Log(Success)
    End Sub
     
  15. freedom2000

    freedom2000 Well-Known Member Licensed User

    Thanks a lot @Erel :)
     
  16. georgelbs

    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?
     
  17. freedom2000

    freedom2000 Well-Known Member Licensed User

    And don't forget to add the delete flag into the rules :

    Code:
    match /public/{allPaths=**}
      {
          allow delete;
      }
    of course with better limits than this...
     
    inakigarm and johndb like this.
  18. Erel

    Erel Administrator Staff Member Licensed User

    It is not needed if writing is allowed.

    B4J doesn't support FirebaseStorage.
     
    johndb likes this.
  19. Filippo

    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?
     
  20. Erel

    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.
     
    Filippo likes this.
Loading...