Home » Android » android – How to make firebase storage reference to a certain bucket?-Exceptionshub

android – How to make firebase storage reference to a certain bucket?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

enter image description here

I create a new bucket in a region near from my users location (asia-southeast1) so hopefully it will fetch the image faster in the client side, I want to upload image and also to download image from that new bucket.

but I don’t how to make reference to that new bucket. usually I use the code below in Android to create reference for default bucket

val ref = FirebaseStorage.getInstance().reference.child("profilePicture")

so how to download and upload image from that bucket? I assume that I have to make some change when creating the reference, but I don’t know how to pointing the reference to that new bucket

How to&Answers:

In the API documentation for FirebaseStorage, it says:

FirebaseStorage is a service that supports uploading and downloading
large objects to Google Cloud Storage. Pass a custom instance of
FirebaseApp to getInstance(FirebaseApp) which will initialize it with
a storage location (bucket) specified via setStorageBucket(String).

Otherwise, if you call getReference() without a FirebaseApp, the
FirebaseStorage instance will initialize with the default FirebaseApp
obtainable from getInstance(). The storage location in this case will
come the JSON configuration file downloaded from the web.

The bolded text is what you will need to do. Initialize a new FirebaseApp instance using a FirebaseOptions object using a builder where you called setStorageBucket() with the name of the bucket you want to use.

Then, when you call FirebaseStorage.getInstance(app), passing that FirebaseApp instance, your references will come from that bucket.

Answer:

After getting info from @samthecodingman and testing it, I can set a reference to use a certain bucket using the code below

FirebaseStorage.getInstance("gs://yourBucketNameHere").reference.child("profilePicture")

Don’t forget to add gs:// in your string as documented here.