javagoogle-cloud-platformgoogle-cloud-storagespring-cloud-gcp

Method threw 'com.google.cloud.storage.StorageException' exception on connection with GCS with JSON key


Getting Storage Exception while connection with GCS !!

<dependencies>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-storage</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>30.0-jre</version>
    </dependency>
</dependencies>

enter image description here


Solution

  • Did some changes in the code and it works

    StorageOptions options = StorageOptions.newBuilder()
                        .setProjectId(PROJECT_ID)
                        .setCredentials(GoogleCredentials.fromStream(new FileInputStream(PATH_TO_JSON_KEY))).build();
                Storage storage = options.getService();
                byte[] bytes = storage.readAllBytes(BlobId.of(BUCKET_NAME, OBJECT_NAME));
    

    use storage.readAllBytes() instead of storage.get()

    My Pom

         <dependencyManagement>
                <dependencies>
                    <dependency>
                        <groupId>com.google.cloud</groupId>
                        <artifactId>libraries-bom</artifactId>
                        <version>24.2.0</version>
                        <type>pom</type>
                        <scope>import</scope>
                    </dependency>
                </dependencies>
            </dependencyManagement>
    
            <dependencies>
                <dependency>
                    <groupId>com.google.cloud</groupId>
                    <artifactId>google-cloud-storage</artifactId>
                </dependency>
    
                <dependency>
                    <groupId>com.google.apis</groupId>
                    <artifactId>google-api-services-storage</artifactId>
                    <version>v1-rev20190129-1.26.0</version>
                </dependency>
    
                <dependency>
                    <groupId>com.google.guava</groupId>
                    <artifactId>guava</artifactId>
                </dependency>
            </dependencies>
    

    use v1-rev20190129-1.26.0 for google-api-services-storage because higher version is not compatible with cloud-storage 2.2.3v