Fernando Bittencourt
Fernando Bittencourt

Reputation: 105

AWS S3 with Localstack return 500 in a Java application

I am developing a Java application with Spring boot. I am using "aws-java-sdk" and I am simulating aws with localstack.

But when I try to insert it into a bucket S3(previously created on the localstack) with the code below, it returns: "(Service: Amazon S3; Status Code: 500; Error Code: 500; Request ID: 33C21AC340BC212F)"

As it is a 500 error I couldn't think of the cause. Please, how can I solve it?

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();
    }

The following variables are loaded by environment variable: region, s3Endpoint, bucketName, awsAccessKey and awsSecretKey.

Upvotes: 0

Views: 2768

Answers (1)

nasser
nasser

Reputation: 79

You should enable the access of path-style

 AmazonS3ClientBuilder.standard()
            .withClientConfiguration(new ClientConfiguration())
            .withEndpointConfiguration(endpointConfig)
            .withCredentials(new AWSStaticCredentialsProvider(credentials))
            .enablePathStyleAccess()
            .build();

Upvotes: 4

Related Questions