简体   繁体   English

使用 firebase 管理员 sdk (java) 在 firebase 存储上上传文件

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

I am trying to upload files using the firebase admin sdk for java.我正在尝试使用 firebase 管理员 sdk 为 java 上传文件。 I followed this official guide https://firebase.google.com/docs/storage/admin/start but when I upload I get the following error:我遵循了这个官方指南https://firebase.google.com/docs/storage/admin/start但是当我上传时出现以下错误:

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.线程“主”中的异常 com.google.cloud.storage.StorageException: firebase-adminsdk-s5n1d@[PROJECT_ID].iam.gserviceaccount.com 没有对 Google Cloud 项目的 serviceusage.services.use 访问权限。 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) 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) 在 com.google.cloud.storage.Bucket.create(Bucket.Z93F725A07423FE1C886FZ:498B33D)D

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]"));

I found to problem:我发现问题:

You just have to remove the "Bucket.BlobWriteOption.userProject("[PROJECT_ID]")" Parameter.您只需删除“Bucket.BlobWriteOption.userProject("[PROJECT_ID]")”参数。

Now it works fine.现在它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM