0

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?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Oirampok
  • 569
  • 5
  • 23
  • Firebase Admin comes with Cloud Storage - no need to add and initialize it separately. If you initialized Firebase Admin, you already have access to the storage APIs by way of the service account you used to initialize it. Read: https://firebase.google.com/docs/storage/admin/start#java – Doug Stevenson Jun 16 '22 at 12:29
  • Thanks!, works perfectly now. Can you put your comment in an answer so i can accept it? – Oirampok Jun 17 '22 at 02:57

1 Answers1

0

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);
Oirampok
  • 569
  • 5
  • 23