首页 > 解决方案 > AWS S3 获取对象版本返回错误代码:500 内部服务器错误

问题描述

检索在 Localstack (docker) 上运行的 S3 中存储的对象版本时出现以下错误:

com.amazonaws.services.s3.model.AmazonS3Exception:内部服务器错误(服务:Amazon S3;状态代码:500;错误代码:500 内部服务器错误;请求 ID:空),S3 扩展请求 ID:空

我已经使用 aws-java-sdk 的 1.11.163、1.11.359、1.11.383 版本进行了测试。

列出和删除文件有效,但我无法使用 SDK 获取版本。

问题是,当使用 cli 时,我可以在我的存储桶中列出对象的版本。

列出我的存储桶中文件的版本时:

aws --endpoint-url= http://localhost:4572 s3api list-object-versions --bucket my-bucket --prefix testFolder

我得到以下输出:

{
"Versions": [
    {
        "LastModified": "2018-08-10T08:52:06.193Z", 
        "VersionId": "0", 
        "ETag": "\"da9a0adf68f7fa4da720e10914283cc9\"", 
        "StorageClass": "STANDARD", 
        "Key": "testFolder/test.txt", 
        "Owner": {
            "DisplayName": "webfile", 
            "ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
        }, 
        "IsLatest": true, 
        "Size": 7366
    }, 
    {
        "LastModified": "2018-08-10T08:52:06.188Z", 
        "VersionId": "0", 
        "ETag": "\"5f22390dd70444e4f3ea347313442a6d\"", 
        "StorageClass": "STANDARD", 
        "Key": "testFolder/0001/test.txt", 
        "Owner": {
            "DisplayName": "webfile", 
            "ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
        }, 
        "IsLatest": true, 
        "Size": 2507
    }, 
    {
        "LastModified": "2018-08-10T08:52:06.191Z", 
        "VersionId": "0", 
        "ETag": "\"9001b739dd280ace7b6b7ae45377484a\"", 
        "StorageClass": "STANDARD", 
        "Key": "testFolder/0001/test-other.txt", 
        "Owner": {
            "DisplayName": "webfile", 
            "ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
        }, 
        "IsLatest": true, 
        "Size": 4589
    }, 
    {
        "LastModified": "2018-08-10T08:52:06.185Z", 
        "VersionId": "0", 
        "ETag": "\"4d32ffe9a1f7e272c78aa80e95fb9cf3\"", 
        "StorageClass": "STANDARD", 
        "Key": "testFolder/test-other.txt", 
        "Owner": {
            "DisplayName": "webfile", 
            "ID": "75aa57f09aa0c8caeab4f8c24e99d10f8e7faeebf76c078efc7c6caea54ba06a"
        }, 
        "IsLatest": true, 
        "Size": 15549
    }
]
}

这是我创建 S3Configuration 的方法:

private static S3Configuration s3Configuration() {
    S3Configuration s3Configuration = new S3Configuration();
    s3Configuration.setBucket("my-bucket");
    return s3Configuration;
}

以下是我创建 AmazonS3 客户端的方法:

private static AmazonS3 amazonS3() {
    final AWSCredentials credentials = new BasicAWSCredentials("foobar","foobar");
    final EndpointConfiguration endpointConfiguration = new EndpointConfiguration("http://localhost:4572",DEFAULT_REGION.getName());

    AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .withEndpointConfiguration(endpointConfiguration)
            .withPathStyleAccessEnabled(true);

    return amazonS3ClientBuilder.build();
}

每当我尝试使用以下命令列出存储桶中文件的版本时,就会发生异常:

VersionListing versionListing = amazonS3.listVersions(new ListVersionsRequest().withBucketName(bucketName));

或尝试获取特定文件的版本:

VersionListing versionListing = amazonS3.listVersions(bucketName, objectKey);

从 S3ObjectSummary.getKey() 中检索 objectKey

知道我缺少什么吗?

谢谢你的帮助。

标签: amazon-web-servicesamazon-s3localstack

解决方案


好像是个bug

这应该可以解决您的问题

new ListVersionsRequest()
                        .withEncodingType("")
                        .withBucketName(bucketName)
                        .withPrefix(key);


推荐阅读