How to correct set endpoint for localstack?

Hello, everybody!

I have to create mock for S3 and I have some issue connection app to S3.
For example, before I had this endpoint for connection to S3 - s3://

Could to help me, how I should set endpoint for connection to localstack?

I have this docker-compose:

    image: localstack/localstack:latest
    network_mode: host
    container_name: localstack
      - AWS_ACCESS_KEY_ID=localstack
      - AWS_SECRET_ACCESS_KEY=localstack
      - SERVICES=s3

In docker’s logs I have this:

2022-08-23T06:27:25.586  INFO --- [  Thread-110] hypercorn.error            : Running on (CTRL + C to quit)
2022-08-23T06:27:25.586  INFO --- [  Thread-110] hypercorn.error            : Running on (CTRL + C to quit)
2022-08-23T06:27:25.888  INFO --- [  MainThread] localstack.utils.bootstrap : Execution of "start_runtime_components" took 1251.06ms
2022-08-23T06:28:52.857  INFO --- [  Thread-123]      : starting moto server on

1 Like

Hi :wave:

You can easily create a boto3 client that interacts with your LocalStack instance. Here is how you can write a script for that purpose:

endpoint_url = ""
# alternatively, to use HTTPS endpoint on port 443:
# endpoint_url = ""

s3_client = boto3.client("s3", endpoint_url=endpoint_url)

def upload_file(file_name, bucket, object_name=None):
    Upload a file to a S3 bucket.
        if object_name is None:
            object_name = os.path.basename(file_name)
        response = s3_client.upload_file(
            file_name, bucket, object_name)
    except ClientError:
        logger.exception('Could not upload file to S3 bucket.')
        return response

Alternatively, if you prefer to (or need to) set the endpoints directly, you can use the $LOCALSTACK_HOSTNAME environment variable which is available when executing user code in LocalStack:

import os
endpoint_url = f"http://{os.getenv("LOCALSTACK_HOSTNAME")}:{os.getenv("EDGE_PORT")}"
client = boto3.client("s3", endpoint_url=endpoint_url)

Thank you so much.

I mean other - how I can connect to my local storage by S3 protocol, not HTTP?)