初始化后更改storageBucket引用 [英] Change storageBucket ref after initialization

查看:50
本文介绍了初始化后更改storageBucket引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为React中的Firebase Storage构建一个存储管理器应用程序.

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.

我的 initializeApp 设置如下

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();

到目前为止,一切都很好.配置工作正常, storage 从配置 storageBucket:'buckettest-xxxxx.appspot.com',

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.

问题是,如果我从应用程序切换到其他存储桶,则 storage 对象仍会从配置中绑定到 buckettest-xxxxx.appspot.com

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

如何在初始化后将 storage 对象切换为使用 buckettest-zzzzz.appspot.com ?

How can I switch the storage object to use the buckettest-zzzzz.appspot.com after the init?

谢谢

推荐答案

最简单的方法可能是创建一个初始化为新存储桶的新 FirebaseApp 对象,然后获取 storage()服务.

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();

这篇关于初始化后更改storageBucket引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆