简体   繁体   English

上传文件到谷歌云存储(JAVA)

[英]Upload file to google cloud storage (JAVA)

I want to upload a file to Google Cloud Storage following the example provided at official documentation我想按照官方文档中提供的示例将文件上传到 Google Cloud Storage

  public class UploadObject {
      public static void uploadObject(
          String projectId, String bucketName, String objectName, String filePath) throws IOException {
        // The ID of your GCP project
        // String projectId = "your-project-id";
    
        // The ID of your GCS bucket
        // String bucketName = "your-unique-bucket-name";
    
        // The ID of your GCS object
        // String objectName = "your-object-name";
    
        // The path to your file to upload
        // String filePath = "path/to/your/file"
    
        Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
        BlobId blobId = BlobId.of(bucketName, objectName);
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
        storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
    
        System.out.println(
            "File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
      }
    }

Nevertheless, I'm getting the error:不过,我得到了错误:

Exception in thread "main" com.google.cloud.storage.StorageException: Error getting access token for service account: 400 Bad Request {"error":"invalid_grant","error_description":"Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim."} at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:227)线程“主”com.google.cloud.storage.StorageException 中的异常:获取服务帐户的访问令牌时出错:400 错误请求 {“error”:“invalid_grant”,“error_description”:“无效 JWT:令牌必须是短lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim."} at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:227)

What could be the cause of this?这可能是什么原因?

This error has two common causes: the clock where you are executing your task is not in sync with NTP (Network Time Protocol), or, you could check how you are handling the refresh tokens and the old tokens.此错误有两个常见原因:您执行任务的时钟与 NTP(网络时间协议)不同步,或者,您可以检查您如何处理刷新令牌和旧令牌。 When the number of refresh tokens exceeds the limit, older tokens become invalid.当刷新令牌的数量超过限制时,旧令牌将失效。 If the application attempts to use an invalidated refresh token, this error is returned.如果应用程序尝试使用无效的刷新令牌,则会返回此错误。 Take a look at this document看看这个文件

On the other hand, there are other options that could help you to upload a file to Cloud Storage, you can find a very clear example about how to do it using Cloud SDK through the command:另一方面,还有其他选项可以帮助您将文件上传到 Cloud Storage,您可以通过以下命令找到有关如何使用 Cloud SDK执行此操作的非常清晰的示例

gsutil cp [OBJECT_LOCATION] gs://[DESTINATION_BUCKET_NAME]/ gsutil cp [OBJECT_LOCATION] gs://[DESTINATION_BUCKET_NAME]/

Here you can find more examples.在这里您可以找到更多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM