How to include a file in a S3 with localstack using Java?

Asked

Viewed 120 times

0

I created an application using Java with Spring. I set up a localstack and created a Bucket using AWS CLI. Everything went right. Only when I try to include a file using the code below, it bursts an error 500.

    @Value("${aws.s3.region}")
    private String region;

    @Value("${aws.s3.endpoint}")
    private String s3Endpoint;

    @Value("${aws.s3.bucket}")
    private String bucketName;

    @Value("${aws.access.key}")
    private String awsAccessKey;

    @Value("${aws.secret.key}")
    private String awsSecretKey;


 public void armazenarArquivo(File arquivo) {
        try {
            AmazonS3 s3Client = criarClienteS3();
            String fileName = arquivo.getName();
            s3Client.putObject(bucketName, fileName, arquivo);
        } catch (AmazonServiceException e) {
            logger.error(e.getMessage());
            throw new RuntimeException();
        } catch (SdkClientException e) {
            logger.error(e.getMessage());
            throw new RuntimeException();
        }
    }

    private AmazonS3 criarClienteS3() {
        AWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
        AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(s3Endpoint, Regions.fromName(region).getName());
        return AmazonS3ClientBuilder.standard()
                  .withEndpointConfiguration(endpoint)
                  .withCredentials(new AWSStaticCredentialsProvider(credentials))
                  .build();
              
    }

Since "aws.S3.endpoint=http://localhost:4566", the same is passed by parameter when creating Bucket with AWS CLI. The returned error falls on the Exception "Amazonserviceexception" with the following message: "(Service: Amazon S3; Status Code: 500; Error Code: 500 ; Request ID: A598CB3EE221ECF6)"

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.