【问题标题】:How to upload an image after changing the default bucket更改默认存储桶后如何上传图片
【发布时间】:2020-06-27 12:56:23
【问题描述】:

我试图将默认存储桶更改为 firestorage,我在互联网上查找了它,我在 firebase 页面上找到了以下代码:

bucket2 = firebase.app().storage(bucket);

但是,之前,我使用的是:

this.afStorage.upload(remakeNewPath, file);

进行上传。现在,如果我这样做:

this.bucket2.upload(remakeNewPath, file);

现在不行了,好像bucket2的值没有this.afStorage那样的属性upload。既然我使用了不同的方式,我将如何上传它?

【问题讨论】:

    标签: javascript firebase google-cloud-storage angularfire2


    【解决方案1】:

    是的,此方法中没有称为上传的功能。

    您需要使用以下代码将图片上传到新路径:

     firebase.storage().ref().child(remakeNewPath).put(file);
    

    这里 remakeNewPath 是路径,您需要在配置中定义用于 firebase 的存储桶。

    【讨论】:

    • 确实解决了上传问题,但现在我不能使用 this.bucket2.ref().getMetaData() 有什么建议吗?
    【解决方案2】:

    documentation for AngularFire2 看来,您可以设置自定义存储桶:

    import {AngularFireStorageModule, BUCKET } from '@angular/fire/storage';
    
    @NgModule({
      providers: [
        { provide: BUCKET, useValue: 'my-bucket-name' }
      ],
      ...
    })
    export class AppModule {}
    

    【讨论】:

    • 我真的不能这样做,因为使用默认存储桶已经完成了,如果我这样做,它将更改在其他地方使用的默认存储桶的所有实例。我只需要在一项特定的服务中更换存储桶,但谢谢
    【解决方案3】:

    您可以使用BUCKET injection token to customize the storage bucket,然后使用following methods to upload blobs 之一:put()putString()upload()

    【讨论】:

    • 我真的不能这样做,因为使用默认存储桶已经完成了,如果我这样做,它将更改在其他地方使用的默认存储桶的所有实例。我只需要在一项特定的服务中更换存储桶,但谢谢
    猜你喜欢
    • 2018-03-01
    • 1970-01-01
    • 2020-09-07
    • 2017-12-23
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多