awss3在java应用程序中使用localstack return500

eulz3vhy  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(167)

我正在用springboot开发一个java应用程序。我正在使用“awsjavasdk”,并用localstack模拟aws。
但是当我尝试用下面的代码将它插入到bucket s3(以前在localstack上创建)中时,它返回:“(服务:amazons3;状态代码:500;错误代码:500;请求id:33c21ac340bc212f)
由于这是一个错误,我想不出原因。拜托,我怎么解决?

public void storeFile(File file) {
        try {
            AmazonS3 s3Client = createS3Client();
            String fileName = file.getName();
            s3Client.putObject(bucketName, fileName, file);
        } catch (AmazonServiceException e) {
            throw new RuntimeException();
        } catch (SdkClientException e) {
            throw new RuntimeException();
        }
    }

    private AmazonS3 createS3Client() {
        AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
        if (s3Endpoint != null) {
            AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(s3Endpoint, region);
            return AmazonS3ClientBuilder.standard()
                    .withEndpointConfiguration(endpoint)
                    .withCredentials(new AWSStaticCredentialsProvider(credentials))
                    .build();
        }
        Regions clientRegion = Regions.fromName(region);
        return AmazonS3ClientBuilder.standard()
                .withRegion(clientRegion)
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .build();
    }

以下变量由环境变量加载:region、s3endpoint、bucketname、awsaccesskey和awssecretkey。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题