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://some.host.ru
Could to help me, how I should set endpoint for connection to localstack?
I have this docker-compose:
localstack:
image: localstack/localstack:latest
network_mode: host
container_name: localstack
environment:
- 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 https://0.0.0.0:4566 (CTRL + C to quit)
2022-08-23T06:27:25.586 INFO --- [ Thread-110] hypercorn.error : Running on https://0.0.0.0:4566 (CTRL + C to quit)
2022-08-23T06:27:25.888 INFO --- [ MainThread] localstack.utils.bootstrap : Execution of "start_runtime_components" took 1251.06ms
Ready.
2022-08-23T06:28:52.857 INFO --- [ Thread-123] l.services.motoserver : starting moto server on http://0.0.0.0:40449
1 Like
Hi
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 = "http://localhost.localstack.cloud:4566"
# alternatively, to use HTTPS endpoint on port 443:
# endpoint_url = "https://localhost.localstack.cloud"
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.
"""
try:
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.')
raise
else:
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)
Hello.
Thank you so much.
I mean other - how I can connect to my local storage by S3 protocol, not HTTP?)