com.amazonaws.services.s3.model.AmazonS3Exception:必须指定用户密钥

时间:2015-06-23 18:05:51

标签: java amazon-s3

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]))

访问/密钥设置正确。

什么是用户密钥

2 个答案:

答案 0 :(得分:2)

  

什么是用户密钥?

答案

用户密钥是用户可以获取凭据的密钥。 It verifies who you are and whether you have permission to access the resources that you are requesting。用户安全凭据包含Access Key IDSecret Access Key

  

您必须将钥匙存放在安全的位置。你的密钥不会   通过AWS管理控制台提供更长时间;你将会有   唯一的副本。保密,以保护您的帐户,   从不发送电子邮件。即使是,也不要在组织外共享它   查询似乎来自AWS或Amazon.com。没有人合法   表示亚马逊会要求您提供密钥。

更多信息:http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSGettingStartedGuide/AWSCredentials.html

您的client.deleteObjects(....);方法具有使用数组处理的键。但下面使用ArrayList删除proper exception handling的对象。

  

删除多个对象(启用版本的存储桶)

  1. 创建AmazonS3Client类的实例。
  2. 创建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

  3. 执行AmazonS3Client.deleteObjects方法。
  4. 以下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. Deleting Multiple Objects Using the AWS SDK for Java
    2. Deleting an Object Using the AWS SDK for Java
    3. Deleting Objects

答案 1 :(得分:1)

如果网址比密钥长,您将提交一个长度超过密钥大小的数组,这些数据可能会导致异常(不会自己尝试)。

你可能想要:

client.deleteObjects(new DeleteObjectsRequest(bucketName)
    .withKeys(keys.toArray(new String[keys.length]))
--------------------------------------^^^^