I am able to run localstack via docker and my docker-compose file looks like:
services:
localstack:
image: localstack/localstack:latest
network_mode: host
environment:
- SERVICES=s3
- AWS_DEFAULT_REGION=eu-west-1
- HOSTNAME_EXTERNAL=localhost
- DEBUG=1
ports:
- '4566-4583:4566-4583'
I am able to create bukcet, upload file via [awslocal][1] like:
create bukcet:
awslocal s3 mb s3://test
> make_bucket: test
upload test file to s3
awslocal s3 cp test.txt s3://test
> upload: ./test.txt to s3://test/test.txt
check if its uploaded:
awslocal s3 ls s3://test
> 2022-12-25 22:18:44 10 test.txt
All I am trying next is to connect via a code. I wrote a simple boto3 python script and the code base is failing with Unable to locate credentials
. I tried aws configure
but considering I don't have any idea what is my access and secret key for localstack s3, it feels like a dead end. The python code base:
import boto3
from botocore.exceptions import ClientError
import os
ddb1 = boto3.client('s3', endpoint_url='http://localhost.localstack.cloud:4566')
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 = ddb1.upload_file(
file_name, bucket, object_name)
except ClientError:
print('Could not upload file to S3 bucket.')
raise
else:
return response
upload_file("testdata/test.txt", "sample")
Any help on how to connect via code base without awslocal
would be a nice help. [1]: https://github.com/localstack/awscli-local
Hi — I would implore you to update your docker-compose.yml
file to this:
version: "3.8"
services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
image: localstack/localstack
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
- DEBUG=${DEBUG-}
- LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-}
- DOCKER_HOST=unix:///var/run/docker.sock
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
The SERVICES
configuration has been deprecated, and all services are lazily loaded right now. I have been able to test the script on my machine, after creating a bucket named sample
, and with the above Compose file, it will work fine!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.