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

1

0

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

Youngs answered 13/1, 2022 at 20:19 Comment(1)
You may want to look in this stack post. Issue is only encountered when running the class in IntelliJ as also mentioned in this link.Finite
Y
1

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

Youngs answered 14/1, 2022 at 20:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Kamilah

© 2022 - 2024 — McMap. All rights reserved.