简体   繁体   中英

Java/Spring using Google Cloud firebase-admin conflicts with google-cloud-storage

I have been using the firebase-admin sdk to authenthicate all requests to a simple REST api running on springboot.

The dependency before was only:

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>8.2.0</version>
</dependency>

and it worked out just fine, i initialized using this class:

@Service
public class FirebaseInitialization {
    public FirebaseInitialization() {
    }

    @PostConstruct
    public void initialization() {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("/serviceAccountKey.json");
            FirebaseOptions options = (new Builder()).setCredentials(GoogleCredentials.fromStream(inputStream)).build();
            FirebaseApp.initializeApp(options);
        } catch (Exception var3) {
            var3.printStackTrace();
        }

    }
}

Now I want to also use the google cloud storage client library to access data in a bucket. I added the depdency as follows:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-storage</artifactId>
    <version>2.8.1</version>
</dependency>

and tried to initialize the client library:

InputStream inputStream = this.getClass().getResourceAsStream("/serviceAccountKey.json");
Credentials credentials = GoogleCredentials.fromStream(inputStream);
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).setProjectId("test").build().getService();

However the program crashes at startup with this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'firestore' defined in class path resource [org/springframework/cloud/gcp/autoconfigure/firestore/GcpFirestoreAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.google.cloud.firestore.Firestore]: Factory method 'firestore' threw exception; nested exception is com.google.cloud.firestore.FirestoreException: java.lang.IllegalStateException: getTransportChannel() called when needsExecutor() is true

What is interesting is that I can fix it by removing either one of the firebase-admin sdk or the cloud storage client library. I tested this by removing the firebase-admin sdk and the cloud storage client library works perfectly.

Is this because they are both accessing the same credentials file? What I know so far is that this is not a permissions issue, because they both work fine independently from one another, the problem just lies when I try and run both at the same time. Maybe there is some config that I need to setup to have both?

Thanks to @Doug Stevenson for the answer.

All I needed to do was remove the cloud-storage client library dependency, continue initializing firebase as usual and then access the bucket through:

Storage storage = StorageClient.getInstance().bucket("test.appspot.com").getStorage();
BlobId blobId = BlobId.of("test", "products/"+productId+".jpg");
return storage.readAllBytes(blobId);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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