更新软件包版本后,Java 函数上传文件到 Google Cloud Storage 失败

问题描述 投票:0回答:1

我有一个maven java项目,它迭代目录的文件并将它们上传到谷歌云存储桶。代码类似于以下内容

public void uploadFiles(String projectId, String bucketName, Path sourceDirectory){
  var storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  try(var files = java.nio.file.Files.walk(sourceDirectory)){
    files.forEach{ file-> {
       var blobId = BlobId.of(bucketName, getBlobPath(file)); \\getBlobPath gets the relative path for the file in google cloud bucket
       var blobInfo = BlobInfo.newBuilder(blobId).build();
       storage.createFrom(blobInfo, file, BlobWriteOption.detectContentType());
      }
    }
  }
}

这是我的pom

    <dependencyManagement>
    <dependencies>
        <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>libraries-bom</artifactId>
          <version>26.19.0</version>
          <type>pom</type>
          <scope>import</scope> 
        </dependency>            
    </dependencies>
</dependencyManagement>
 #All google libraries I am using in various parts of the project
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-storage</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-nio</artifactId>
    </dependency>

这工作正常,但如果我将库版本更新到最新版本(26.22.0)或任何更高版本(26.20.0),则上传失败并出现以下错误(我已从错误消息中删除了网址)

com.google.cloud.storage.StorageException: Client side data loss detected. Attempt to append to a resumable session with an offset higher than the backend has
|> PUT https://storage.googleapis.com/upload/storage/v1/b/......
|> content-range: bytes 0--1/0
|  
|< HTTP/1.1 400 Bad Request
|< content-length: 37
|< content-type: text/plain; charset=utf-8
|< x-guploader-uploadid: 
|< 
|< Failed to parse Content-Range header.
|  
at com.google.cloud.storage.JsonResumableSessionFailureScenario.toStorageException(JsonResumableSessionFailureScenario.java:185)
at com.google.cloud.storage.JsonResumableSessionFailureScenario.toStorageException(JsonResumableSessionFailureScenario.java:117)
at com.google.cloud.storage.JsonResumableSessionPutTask.call(JsonResumableSessionPutTask.java:204)
at com.google.cloud.storage.JsonResumableSession.lambda$put$0(JsonResumableSession.java:81)
at com.google.cloud.storage.Retrying.lambda$run$0(Retrying.java:102)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:103)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
at com.google.cloud.storage.Retrying.run(Retrying.java:99)
at com.google.cloud.storage.JsonResumableSession.put(JsonResumableSession.java:68)
at com.google.cloud.storage.StorageImpl.createFrom(StorageImpl.java:258)
at com.google.cloud.storage.StorageImpl.createFrom(StorageImpl.java:224)

搜索后我找不到任何解释或有类似问题的人。任何帮助表示赞赏。谢谢

java maven google-cloud-platform google-cloud-storage
1个回答
0
投票

可以通过更改为使用输入流而不是源路径来使其工作。

public void uploadFiles(String projectId, String bucketName, Path sourceDirectory){
  var storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
  try(var files = java.nio.file.Files.walk(sourceDirectory)){
    files.forEach{ file-> {
       var blobId = BlobId.of(bucketName, getBlobPath(file)); \\getBlobPath gets the relative path for the file in google cloud bucket
       var blobInfo = BlobInfo.newBuilder(blobId).build();
       storage.createFrom(blobInfo, new BufferedInputStream(Files.newInputStream(file)), BlobWriteOption.detectContentType());
    }
   }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.