com.amazonaws.services.s3.model.AmazonS3Exception: User key must be specified. (Service: Amazon S3; Status Code: 400;
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1160)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:748)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:467)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:302)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3769)
at com.amazonaws.services.s3.AmazonS3Client.deleteObjects(AmazonS3Client.java:1841)
代码如下所示:
client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys.toArray(new String[urls.length]))
访问/密钥设置正确。
什么是用户密钥?
答案 0 :(得分:2)
什么是用户密钥?
答案
用户密钥是用户可以获取凭据的密钥。 It verifies who you are and whether you have permission to access the resources that you are requesting
。用户安全凭据包含Access Key ID
和Secret Access Key
。
您必须将钥匙存放在安全的位置。你的密钥不会 通过AWS管理控制台提供更长时间;你将会有 唯一的副本。保密,以保护您的帐户, 从不发送电子邮件。即使是,也不要在组织外共享它 查询似乎来自AWS或Amazon.com。没有人合法 表示亚马逊会要求您提供密钥。
更多信息:http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html
您的client.deleteObjects(....);
方法具有使用数组处理的键。但下面使用ArrayList
删除proper exception handling
的对象。
删除多个对象(启用版本的存储桶)
AmazonS3Client
类的实例。创建DeleteObjectsRequest
类的实例并提供
list of objects keys
以及可选的对象版本ID
你要删除。
如果指定要删除的对象的版本ID,请Amazon S3 deletes the specific object version
。如果未指定要删除的对象的版本ID,请Amazon S3 adds a delete marker
。有关详细信息,请参阅Deleting One Object Per Request。
AmazonS3Client.deleteObjects
方法。以下Java代码示例演示了上述步骤。
List<KeyVersion> keys = new ArrayList<KeyVersion>();
// Provide a list of object keys and versions.
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(bucketName)
.withKeys(keys);
try {
DeleteObjectsResult delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
System.out.format("Successfully deleted all the %s items.\n", delObjRes.getDeletedObjects().size());
} catch (MultiObjectDeleteException e) {
// Process exception.
}
对于多对象删除(非版本化存储桶),您可以使用此方法 还
static void multiObjectNonVersionedDelete(List<KeyVersion> keys) {
// Multi-object delete by specifying only keys (no version ID).
DeleteObjectsRequest multiObjectDeleteRequest = new DeleteObjectsRequest(
bucketName).withQuiet(false);
// Create request that include only object key names.
List<KeyVersion> justKeys = new ArrayList<KeyVersion>();
for (KeyVersion key : keys) {
justKeys.add(new KeyVersion(key.getKey()));
}
multiObjectDeleteRequest.setKeys(justKeys);
// Execute DeleteObjects - Amazon S3 add delete marker for each object
// deletion. The objects no disappear from your bucket (verify).
DeleteObjectsResult delObjRes = null;
try {
delObjRes = s3Client.deleteObjects(multiObjectDeleteRequest);
System.out.format("Successfully deleted all the %s items.\n", delObjRes.getDeletedObjects().size());
} catch (MultiObjectDeleteException mode) {
printDeleteResults(mode);
}
}
有关示例的详细信息,您可以按照教程
进行操作答案 1 :(得分:1)
如果网址比密钥长,您将提交一个长度超过密钥大小的数组,这些数据可能会导致异常(不会自己尝试)。
你可能想要:
client.deleteObjects(new DeleteObjectsRequest(bucketName)
.withKeys(keys.toArray(new String[keys.length]))
--------------------------------------^^^^