问题
I am trying to build a storage manager app for Firebase Storage in React.
The app should be able to list/add/delete buckets and files inside them.
I have my initializeApp
set up as follows
import firebase from 'firebase';
import 'firebase/auth';
import 'firebase/storage';
firebase.initializeApp({
apiKey: 'xxxxxxxxxxxxxxxxxxxx',
authDomain: 'buckettest-xxxxx.firebaseapp.com',
projectId: 'buckettest-xxxxx',
storageBucket: 'buckettest-xxxxx.appspot.com',
messagingSenderId: 'xxxxxxxxxxxxxxxxxxxx',
appId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
measurementId: 'G-QZDBHEWHJ5',
});
const auth = firebase.auth();
const storage = firebase.storage();
So far so good. The config works, the storage
is connected to the storage bucket from the config storageBucket: 'buckettest-xxxxx.appspot.com',
Upload, download, view, delete functionalities work perfect.
The problem is that if I switch to a different bucket from my app, the storage
object is still bound to the buckettest-xxxxx.appspot.com
from the config.
How can I change the bucket so that I can upload to other buckets as well?
Assume I have the following list of buckets:
- buckettest-xxxxx.appspot.com
- buckettest-yyyyy.appspot.com
- buckettest-zzzzz.appspot.com
How can I switch the storage
object to use the buckettest-zzzzz.appspot.com
after the init?
Thanks
回答1:
The easiest way is probably to create a new FirebaseApp
object initialized to the new bucket, and then get the storage()
service from that.
var otherApp = firebase.initializeApp({
storageBucket: 'otherbucket.appspot.com',
}, second);
var otherStorage = otherApp.storage();
来源:https://stackoverflow.com/questions/65280088/change-storagebucket-ref-after-initialization