Amazon S3存储桶以第三方编程方式拥有

时间:2015-12-18 15:32:24

标签: java amazon-web-services amazon-s3

我是S3的新手。我们的供应商之一与我们共享一个桶和对象。我们创建了一个AWS账户,并将我们的团队成员添加为用户。我们可以通过amazon aws cli访问存储桶中的数据。我正在寻找java api以编程方式访问数据。

我创建了一个独立的代码

    /*
     * Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) 
     * and save the following lines after replacing the underlined values with your own.
     *
     * [default]
     * aws_access_key_id = YOUR_ACCESS_KEY_ID
     * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
     */

    AmazonS3 s3 = new AmazonS3Client();
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    s3.setRegion(usEast1); 
    System.out.println("Listing objects");
        ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
                .withBucketName("exports.abx.t-z/xyz")
                );
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " +
                    "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();

现在我遇到了异常

    Listing objects
Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.
Error Message:    The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey; Request ID: C80B0460828347D0)
HTTP Status Code: 404
AWS Error Code:   NoSuchKey
Error Type:       Client
Request ID:       C80B0460828347D0

我没有访问密钥。任何帮助表示赞赏。

谢谢, 阿米特

1 个答案:

答案 0 :(得分:2)

404响应表明您的凭据没问题,但您要求的内容不存在。

这部分代码看起来很可疑:

new ListObjectsRequest().withBucketName("exports.abx.t-z/xyz")

应为withBucketName方法指定存储桶名称。我怀疑在你的情况下,存储桶名称应该只是“exports.abx.t-z”。如果您要列出该存储桶中“xyz”“文件夹”中的所有对象,那么您可能希望执行以下操作:

new ListObjectsRequest().withBucketName("exports.abx.t-z").withPrefix("xyz")