【发布时间】:2020-07-12 13:18:04
【问题描述】:
我想按照官方documentation提供的示例将文件上传到谷歌云存储
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);
}
}
但是,我得到了错误:
线程“main”com.google.cloud.storage.StorageException 中的异常:获取服务帐户的访问令牌时出错:400 Bad Request {"error":"invalid_grant","error_description":"Invalid JWT:令牌必须是短期令牌(60 分钟)并且在合理的时间范围内。请检查 JWT 声明中的 iat 和 exp 值。"} at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc. java:227)
这可能是什么原因?
【问题讨论】:
-
欢迎来到 StackOverflow。查看how to ask a good question 上的指南以了解未来的问题。看起来(来自 cmets)您在询问之前已经尝试了一些事情。包括您尝试过的内容(使用minimal, reproducable example 代码)可以帮助人们更快更好地帮助您。
标签: java google-cloud-storage cloud cloud-storage