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: 745
Last edited:
D

Deleted member 103

Guest
Hi,
I try to time this example, of course, I changed the "bucket" and Pakage name, but there is always at "Downoad Public" this error message:
(StorageException) com.google.firebase.storage.StorageException: User does not have permission to access this object.
Firebase-storage.png

Firebase-storage_2.png
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Sorry Erel, but I do not understand where and how. :(
Go in forebase console
select your project
click on storage
select "rules tab"

project package name is pointed by the arrow

rules.jpg


add the rules you want
Here is says :
everybody can read
everybody can write only in public/field1/fileId path provided that the file is less than 45 bytes (which is quite small !!!) and its name is 1.txt
 
D

Deleted member 103

Guest
Hi freedom2000 ,

my rulle looks but does not work anyway.
Firebase-storage_rulle.png
 

freedom2000

Well-Known Member
Licensed User
Longtime User
With your rule you must be authenticated (look for firebase Auth)

Just to be sure, you should try with

allow read, write

without any restriction
 
D

Deleted member 103

Guest
without any restriction ?
Then everyone can read and change the data of all, or I did not understand for what firebase well. :(
 

freedom2000

Well-Known Member
Licensed User
Longtime User
No it was just for you to be sure that it worked !
Just to check your configuration
 

freedom2000

Well-Known Member
Licensed User
Longtime User
by the way I think that you are not allowed to write at the root of firebase

you should add a path
match /something/{allPaths**}
 
D

Deleted member 103

Guest
Error message:
Activity (main) Resume **(StorageException) com.google.firebase.storage.StorageException: Object does not exist at location.

B4X:
Sub btnDownloadPublic_Click
    'You need to first upload a file from Firebase console.
    Starter.storage.DownloadFile("/public/somefile", File.DirInternalCache, "out.txt")
End Sub

FirebaseStorage:
Firebase-storage_3.png


The rules is the same as from the first post, only with my bucket.

Note, I use your example of the first page, I just changed the bucket and "google-services.json files".
 
D

Deleted member 103

Guest
have you tried
B4X:
Starter.storage.DownloadFile("/public/somefile/out.txt", File.DirInternalCache, "out.txt")
In your code you did not specify which file you want to download... You just set a path
Danke Manfred!
Ich habe es gewusst dass es nur eine kleinigkeit sein kann. ;)
Ich habe einfach den Beispiel von Erel verwendet ohne Änderung.
Jetzt bleibt nur noch die Frage, warum die Authentifizierung nicht funktioniert?

Thanks Manfred!
I knew it that it can only be a trifle. ;)
I have just the example of Erel used without modification.
Now the question remains, why the authentication does not work?
 
Status
Not open for further replies.
Top