【发布时间】:2016-08-29 18:04:17
【问题描述】:
我正在使用 Google Cloud Platform 的 Java 客户端将对象从存储桶中复制到同一存储桶中的不同位置。
为此,我遵循了here 的说明,但我得到了GoogleJsonResponseException 404 Bad Request:
class com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Required",
"reason" : "required"
} ],
"message" : "Required"
}
这是我的代码:
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("key.json"))
.createScoped(Collections.singleton(StorageScopes.CLOUD_PLATFORM));
storageClient = new Storage.Builder(httpTransport, jsonFactory, credential).build();
StorageObject newObject = new StorageObject();
newObject.setName(newName);
Storage.Objects.Copy request = storageClient.objects().copy(bucketName, objectName, bucketName, newName, newObject);
request.setDestinationPredefinedAcl("publicread");
request.execute();
到目前为止,这是唯一失败的操作,上传、下载、getLength 和其他一些操作都很好。
更新:
我设法使它工作删除这条线:
request.setDestinationPredefinedAcl("publicread");
无论如何,我想知道出了什么问题,因为将来我希望能够选择新对象是公共的还是私有的。
【问题讨论】:
-
在我看来,在调用请求时,您缺少必填字段或未正确传递。
-
我认为第一个参数应该是 Data.Object 字段。在您从谷歌网站提供的示例链接中的“内容”。
-
@lsiva 感谢您的回答,我设法使其部分工作并更新了问题。
-
GCS 中存在一个临时配置问题,该问题干扰了对 JSON API 的无正文 POST 请求。您现在应该能够按照最初的说明发出请求。您还应该能够省略整个 newObject 并传递 null,因为除了通过参数提供的名称之外您没有设置任何其他内容。
-
如果内容没有变化,则将其传递为空。复制操作将起作用。
标签: java google-api google-cloud-storage bad-request