Android Question firebase storage "file metadata"

freedom2000

Well-Known Member
Licensed User
Longtime User
Hi,

I would like to use file metadata to limit access to group users to certains files

It seems to be easily feasible with firebase storage : Firebase Use file meta data

I have looked at the example provided in this link , but haven't found a way to access/modify file meta data.

Is there an easy way to do it please ?
 

freedom2000

Well-Known Member
Licensed User
Longtime User
I haven't find any documentation about the group id. Did you see where the groups are defined?

For the groups I haven't found a way to identify them with an API. It seems that only the console allows this.

However, a way to identify a "community" is to use files metadata.
And they provide API to get and modify them by software

B4X:
// Create file metadata including the content type
StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType("image/jpg")
        .setCustomMetadata("myCustomProperty", "myValue")
        .build();

// Update metadata properties
forestRef.updateMetadata(metadata)
        .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
            @Override
            public void onSuccess(StorageMetadata storageMetadata) {
                // Updated metadata is in storageMetadata
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Uh-oh, an error occurred!
            }
        });


Then metadata could be used to securely limit access to this "group" :

B4X:
// Allow reads if the group ID in your token matches the file metadata's `owner` property
// Allow writes if the group ID is in the user's custom token
match /files/{groupId}/{fileName} {
  allow read: if resource.metadata.owner == request.auth.token.groupId;
  allow write: if request.auth.token.groupId == groupId;
}

I must say that firebase documentation is not always clear for me !
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Yes sure but I don' know how to do all this with B4A

firebaseauth seems to only give access to users token that I cannot share between users...

So I tought that the best way was to handle a kind of grouId with metadata :
B4X:
// Create file metadata including the content type
StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType("image/jpg")
        .build();

// Upload the file and metadata
uploadTask = storageRef.child("images/mountains.jpg").putFile(file, metadata);

My basic need is to exchange files via firebasestorage with only some specifics users with just a "specific Key" known by them.
And I don't know how to do it !
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is nothing that you need to do in B4A. You need to add the rule in Firebase console and just use the correct path.

Note that you have another option.

User cannot see the available folders. So just create a folder with a "random" name and only the relevant users will know its path.
 
Upvote 0

freedom2000

Well-Known Member
Licensed User
Longtime User
Note that you have another option.

User cannot see the available folders. So just create a folder with a "random" name and only the relevant users will know its path.

This is exactly the KISS(*) paradigm that I was looking for

KISS : Keep It Simple and Stupid

Thank you, it answers elegantly to my question
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…