如何将Firebase Admin SDK与存储一起使用

时间:2018-01-24 21:42:13

标签: java firebase firebase-storage firebase-admin

我需要使用Firebase Admin SDK将文件添加到存储中。这似乎有问题,我无法弄明白该怎么做。这是我的初始化代码:

FileInputStream serviceAccount = new FileInputStream("path/to/my/file.json");
FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(GoogleCredentials.fromStream(serviceAccount))
        .setDatabaseUrl("https://my-app.firebaseio.com")
        .setStorageBucket("gs://my-app.appspot.com")
        .build();

FirebaseApp.initializeApp(options);

然后,我从这段代码中得到了我的桶:

Bucket bucket = StorageClient.getInstance().bucket();

但每次,我都会收到此错误:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient$Builder.setBatchPath(Ljava/lang/String;)Lcom/google/api/client/googleapis/services/AbstractGoogleClient$Builder;
at com.google.api.services.storage.Storage$Builder.setBatchPath(Storage.java:9263)
at com.google.api.services.storage.Storage$Builder.<init>(Storage.java:9242)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.<init>(HttpStorageRpc.java:94)
at com.google.cloud.storage.StorageOptions$DefaultStorageRpcFactory.create(StorageOptions.java:54)
at com.google.cloud.storage.StorageOptions$DefaultStorageRpcFactory.create(StorageOptions.java:48)
at com.google.cloud.ServiceOptions.getRpc(ServiceOptions.java:472)
at com.google.cloud.storage.StorageOptions.getStorageRpcV1(StorageOptions.java:122)
at com.google.cloud.storage.StorageImpl.<init>(StorageImpl.java:99)
at com.google.cloud.storage.StorageOptions$DefaultStorageFactory.create(StorageOptions.java:44)
at com.google.cloud.storage.StorageOptions$DefaultStorageFactory.create(StorageOptions.java:38)
at com.google.cloud.ServiceOptions.getService(ServiceOptions.java:459)
at com.google.firebase.cloud.StorageClient.getInstance(StorageClient.java:62)
at management.AdminService.<init>(AdminService.java:29)
at Main.main(Main.java:17)

有什么问题? :带有存储空间的谷歌管理员SDK教程似乎很神秘。

1 个答案:

答案 0 :(得分:1)

当您调用setStorageBucket()时,您应该只传递存储桶的名称,而不是gs URL:

.setStorageBucket("my-app.appspot.com")

但是,如果我使用gs://,我收到的错误消息与您的不同。我正在使用SDK的5.8.0版本。

你是对的,文档不是很清楚。我会在团队内部提出一个问题。