I was trying to upload file to GCS in Java.

Below is the way I create the Storage object:

StorageOptions.Builder storageOptions = StorageOptions.newBuilder().setProjectId(AppProperties.get(GCS_PROJECT_ID));

GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(keyFilePath)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));

Storage storage = storageOptions.setCredentials(credentials).build().getService();

When I tried to do storage.create(blobInfo, Files.readAllBytes(Paths.get(dataFolder + "/" + fileName))); I got the following error:

com.google.cloud.storage.StorageException: Unexpected error refreshing access token
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:233)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:314)
    at com.google.cloud.storage.StorageImpl$3.call(StorageImpl.java:221)
    at com.google.cloud.storage.StorageImpl$3.call(StorageImpl.java:218)
    at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
    at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
    at com.google.cloud.storage.StorageImpl.internalCreate(StorageImpl.java:217)
    at com.google.cloud.storage.StorageImpl.create(StorageImpl.java:171)

Caused by: java.io.IOException: Unexpected error refreshing access token
    at com.google.auth.oauth2.OAuth2Credentials.unwrapDirectFuture(OAuth2Credentials.java:309)
    at com.google.auth.oauth2.OAuth2Credentials.getRequestMetadata(OAuth2Credentials.java:156)
    at com.google.auth.oauth2.ServiceAccountCredentials.getRequestMetadata(ServiceAccountCredentials.java:962)
    at com.google.auth.http.HttpCredentialsAdapter.initialize(HttpCredentialsAdapter.java:96)
    at com.google.cloud.http.HttpTransportOptions$1.initialize(HttpTransportOptions.java:159)
    at com.google.cloud.http.CensusHttpModule$CensusHttpRequestInitializer.initialize(CensusHttpModule.java:109)
    at com.google.api.client.http.HttpRequestFactory.buildRequest(HttpRequestFactory.java:91)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:521)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.create(HttpStorageRpc.java:311)
    ... 14 more
Caused by: java.lang.NoSuchMethodError: com.google.common.io.ByteStreams.exhaust(Ljava/io/InputStream;)J
    at com.google.api.client.http.ConsumingInputStream.close(ConsumingInputStream.java:40)
    at java.util.zip.InflaterInputStream.close(InflaterInputStream.java:227)
    at java.util.zip.GZIPInputStream.close(GZIPInputStream.java:136)
    at sun.nio.cs.StreamDecoder.implClose(StreamDecoder.java:378)
    at sun.nio.cs.StreamDecoder.close(StreamDecoder.java:193)
    at java.io.InputStreamReader.close(InputStreamReader.java:199)
    at com.google.gson.stream.JsonReader.close(JsonReader.java:1224)
    at com.google.api.client.json.gson.GsonParser.close(GsonParser.java:53)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:363)
    at com.google.api.client.json.JsonParser.parse(JsonParser.java:335)
    at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:79)
    at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:73)
    at com.google.api.client.http.HttpResponse.parseAs(HttpResponse.java:456)
    at com.google.auth.oauth2.ServiceAccountCredentials.refreshAccessToken(ServiceAccountCredentials.java:618)
    at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:243)
    at com.google.auth.oauth2.OAuth2Credentials$1.call(OAuth2Credentials.java:240)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:457)
    at com.google.auth.oauth2.OAuth2Credentials$AsyncRefreshResult.executeIfNew(OAuth2Credentials.java:567)
    at com.google.auth.oauth2.OAuth2Credentials.asyncFetch(OAuth2Credentials.java:206)
    ... 24 more

I verified that the key file is valid since the same key file works perfectly in airflow to upload file in Python

The pom is as below:

<dependencies>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud</artifactId>
            <version>0.8.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.collections</groupId>
            <artifactId>google-collections</artifactId>
            <version>1.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1.1-jre</version>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>libraries-bom</artifactId>
                <version>20.8.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

This only happens in Intellij. I tried directly executing the jar on command line and it works for me

Could someone please help with this?

0

There are 0 best solutions below