0

I have installed S3 on my LocalStack instance. I am attempting to access data I have inserted into some buckets on my S3 instance using a Lambda Function. The code that I use to do this is the following:

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials()))
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("s3.localhost.localstack.cloud:4566",
                Regions.US_EAST_1.getName()))
        .build();

S3Object s3Object = s3Client.getObject("bucketName", "Key.sql");

The code I use to set the credentials is the following:

public AWSCredentials credentials() {
    AWSCredentials credentials = new BasicAWSCredentials(
            "test",
            "test"
    );
    return credentials;
}

From my Docker instance in the Docker LocalStack container, I go to the command line for the terminal and I run the following command:

awslocal lambda invoke --function-name optimization-data-loader /dev/stdout

When the execution runs, I receive the following result:

Unable to execute HTTP request: Connect to bucketName.localhost.localstack.cloud:4566 [bucketName.s3.localhost.localstack.cloud/127.0.0.1] failed: Connection refused (Connection refused)

I know for a fact that the S3 bucket is active and has data within it because I can access it directly via my browser.

2
  • The "Connection refused (Connection refused)" clearly shows that either nothing is listening on host bucketName.localhost.localstack.cloud:4566, or something like a firewall is actively refusing the connection. What URL are you using in your browser? Commented Oct 16, 2023 at 15:06
  • @MarkRotteveel I am using bucketName.s3.localhost.localstack.cloud:4566. Which work as I would expect. I also see this error when executing the Lambda function from within my IntelliJ IDE. I know IntelliJ starts a new docker container when i execute Lambda function within debug mode. Commented Oct 16, 2023 at 17:26

1 Answer 1

0

I had a similar issue and resolved it by creating the S3Client this way :

`AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials())).endpointOverride(URI.create("https://localhost.localstack.cloud:4566"))
            .forcePathStyle(true)
            .region(Region.US_EAST_1).build();`

The forcePathStyle was the key.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.