从GCP存储桶读取文件的NoSuchMethod

时间:2019-09-19 10:36:17

标签: java google-cloud-platform

我正在尝试使用此代码从Google Cloud Storage存储桶中读取文件

Blob blob = storage.get(BlobId.of(Constants.GCS_BUCKET, srcFilename));
printBlob(blob.reader());
private void printBlob(ReadChannel reader) {
    try {
        WritableByteChannel outChannel = Channels.newChannel(System.out);
        ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
        while (reader. read(bytes) > 0) {
            bytes.flip();
            outChannel.write(bytes);
            bytes.clear();
        }
    }
    catch (Exception e) {

    }

}

执行reader.read(bytes)时,代码将引发此异常

[INFO] GCLOUD: Caused by: 
[INFO] GCLOUD: java.lang.NoSuchMethodError: com.google.api.services.storage.Storage$Objects$Get.setReturnRawInputStream(Z)Lcom/google/api/client/googleapis/services/AbstractGoogleClientRequest;
[INFO] GCLOUD:  at com.google.cloud.storage.spi.v1.HttpStorageRpc.createReadRequest(HttpStorageRpc.java:658)
[INFO] GCLOUD:  at com.google.cloud.storage.spi.v1.HttpStorageRpc.read(HttpStorageRpc.java:693)
[INFO] GCLOUD:  at com.google.cloud.storage.BlobReadChannel$1.call(BlobReadChannel.java:127)
[INFO] GCLOUD:  at com.google.cloud.storage.BlobReadChannel$1.call(BlobReadChannel.java:124)

1 个答案:

答案 0 :(得分:1)

已解决。 需要使用依赖项

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-core-http</artifactId>
        <version>1.91.0</version>
    </dependency>

如果使用1.90.0,它将崩溃。