How to make firebase storage reference to a certain bucket?

感情迁移 提交于 2020-02-26 04:05:07

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/60274587/how-to-make-firebase-storage-reference-to-a-certain-bucket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!