Upload file on firebase storage using the firebase admin sdk (java)

huangapple go评论63阅读模式
英文:

Upload file on firebase storage using the firebase admin sdk (java)

问题

我正在尝试使用Firebase Admin SDK for Java上传文件。我遵循了这个官方指南:https://firebase.google.com/docs/storage/admin/start,但是当我上传时,我遇到了以下错误:

在主线程中的异常"main" com.google.cloud.storage.StorageException: firebase-adminsdk-s5n1d@[PROJECT_ID].iam.gserviceaccount.com没有serviceusage.services.use访问Google Cloud项目。
在com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:227)中转换
在com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:308)中创建
在com.google.cloud.storage.StorageImpl.create(StorageImpl.java:189)中
在com.google.cloud.storage.Bucket.create(Bucket.java:987)中

对我来说,这听起来像是我没有权限,但根据指南,我应该获得对存储桶的经过身份验证的引用...

这是代码:

        FileInputStream serviceAccount =
                new FileInputStream("XXX.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://[PROJECT_ID].firebaseio.com")
                .setStorageBucket("[PROJECT_ID].appspot.com")
                .build();

        FirebaseApp.initializeApp(options);

        InputStream file = new FileInputStream(filePath);
        Blob blob = StorageClient.getInstance().bucket().create(fileName, file, Bucket.BlobWriteOption.userProject("[PROJECT_ID]"));
英文:

I am trying to upload files using the firebase admin sdk for java. I followed this official guide https://firebase.google.com/docs/storage/admin/start but when I upload I get the following error:

Exception in thread "main" com.google.cloud.storage.StorageException: firebase-adminsdk-s5n1d@[PROJECT_ID].iam.gserviceaccount.com does not have serviceusage.services.use access to the Google Cloud project.
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:227)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:308)
at com.google.cloud.storage.StorageImpl.create(StorageImpl.java:189)
at com.google.cloud.storage.Bucket.create(Bucket.java:987)

This sounds to me like I am not authorised, but according to the guide I should get an authenticated reference to the bucket...

This is the code:

        FileInputStream serviceAccount =
                new FileInputStream("./XXX.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://[PROJECT_ID].firebaseio.com")
                .setStorageBucket("[PROJECT_ID].appspot.com")
                .build();

        FirebaseApp.initializeApp(options);

        InputStream file = new FileInputStream(filePath);
        Blob blob = StorageClient.getInstance().bucket().create(fileName, file, Bucket.BlobWriteOption.userProject("[PROJECT_ID]"));

答案1

得分: 3

我找到了问题:

你只需要移除 "Bucket.BlobWriteOption.userProject("[PROJECT_ID]")" 参数。

现在它可以正常工作。

英文:

I found to problem:

You just have to remove the "Bucket.BlobWriteOption.userProject("[PROJECT_ID]")" Parameter.

Now it works fine.

huangapple
  • 本文由 发表于 2020年7月31日 15:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63187562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定