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

johndb

Active Member
Licensed User
Longtime User
I am using Firebase storage and it works like a charm. Is there a way of getting the timestamp of a particular file in storage?

Thanks,

John
 

inakigarm

Well-Known Member
Licensed User
Longtime User
B4X:
StorageMetadata.Timestamp
(returns Ticks from file's last update time) Prior you have to Dim a StorageMetadata Object
 

johndb

Active Member
Licensed User
Longtime User
B4X:
StorageMetadata.Timestamp
(returns Ticks from file's last update time) Prior you have to Dim a StorageMetadata Object
Thank you. I'm assuming that the StorageMetaData that you mention is returned in the event "Storage_MetadataCompleted (Metadata As StorageMetadata, Success As Boolean)" but how do I specify the "serverpath" in "storage.GetMetadata(serverpath as string)"?

[SOLVED] The server path is a string with the path of the file. I was attempting to prefix the actual path with the storage bucket and this is wrong. Simply specify a path. In this case it is a private folder so the path would be "/user/<user uid>/<filename>".
 
Last edited:

sunish

Member
Licensed User
Longtime User
I was trying the firebasestorage sample following the instructions. I put the json file obtained from console in the B4a folder. However I get an error "Error parsing manifest script:Line26, Word = C command expected." This prevents further compilation.

The corresponding line of the manifest file is
B4X:
CreateResourceFromFile("google-services", "google-services.json")


Sorry : Problem solved. I was accidentally using the previous version of B4A installed on the same machine.
 

sunish

Member
Licensed User
Longtime User
While trying the sample code, sign in doesn't seem to work.
I have enabled authentication in the console and am able to download the shared file in public folder, but google sign in button just results in "sending message to waiting queue (OnActivityResult)"
I get the prompt to select a google account, but nothing happens on pressing any of the Google accounts other than the above message.

I got cloud messaging to work and guess the creation of the json file etc is working fine as I can also download from the public folder, which implies the cloud storage url is set right.
 

sunish

Member
Licensed User
Longtime User
While trying the sample code, sign in doesn't seem to work.
I have enabled authentication in the console and am able to download the shared file in public folder, but google sign in button just results in "sending message to waiting queue (OnActivityResult)"
I get the prompt to select a google account, but nothing happens on pressing any of the Google accounts other than the above message.

I got cloud messaging to work and guess the creation of the json file etc is working fine as I can also download from the public folder, which implies the cloud storage url is set right.

Okay found the problem, any kind of authentication requires a SHA1 fingerprint and will not work with default debug key
 

desof

Well-Known Member
Licensed User
Longtime User
Hi, I can connect but when I click on any of the buttons the application closes immediately.
What am I doing wrong ?

This is the detail of the log in Debug mode

Installing file.
PackageAdded: package:Test.addies
Copying updated assets files (4)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Signed in: com.google.android.gms.internal.zzbkh@3b62544
Error occurred on line: 53 (Main)
java.lang.IllegalArgumentException: The supplied bucketname is not available to this project.
at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.zzz(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getReferenceFromUrl(Unknown Source)
at anywheresoftware.b4x.objects.FirebaseStorageWrapper.UploadStream(FirebaseStorageWrapper.java:63)
at anywheresoftware.b4x.objects.FirebaseStorageWrapper.UploadFile(FirebaseStorageWrapper.java:56)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:343)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 

trueboss323

Active Member
Licensed User
Longtime User
Will this work if I want to synchronize Statemanager settings from one phone to another? And which of the folder (auth, public, user) would this be best for? Or would the CloudKVS library be better?
 

freedom2000

Well-Known Member
Licensed User
Longtime User
Will this work if I want to synchronize Statemanager settings from one phone to another? And which of the folder (auth, public, user) would this be best for? Or would the CloudKVS library be better?
It does work to sync phones (statemanager or anything else)
You would better choose a non public folder...
 
Last edited:

trueboss323

Active Member
Licensed User
Longtime User
It does work to sync phones (statemanager or anything else)
You would better choose a non public folder...

Okay. In my case I want data and settings in my app to be synchronized across all devices. So if a user gets a new phone or something, then their settings will automatically be restored, or something like that.

My other question is if you able to modify their settings? Say a user emails you for help asking to change a score or setting for them , would you be able to do that?
 
Status
Not open for further replies.
Top