Change storageBucket ref after initialization

≯℡__Kan透↙ 提交于 2020-12-15 05:32:26

问题


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

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