我正在尝试通过使用此代码从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)

最佳答案

解决了。
需要使用依赖

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


如果使用1.90.0,它将崩溃。

关于java - 从GCP存储桶读取文件的NoSuchMethod,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58009110/

10-16 00:59