简体   繁体   中英

Using boto3 through SAM local to interact with Localstack S3

I am having a strange issue trying to get boto3 in AWS SAM local to connect to localstack S3 . I know this is especially strange because SQS works fine. Both localstack and AWS SAM local are being invoked to run within a Docker network I've created, called test .

My (non-working S3) Python code is:

  ACCESS_KEY='123'
  SECRET_KEY='abc'
  s3 = boto3.client('s3',
                      endpoint_url="http://docker.for.mac.localhost:4572",
                      use_ssl=False,
                      aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)
  print s3.list_buckets()

The above code gives me the error: An error occurred (NoSuchBucket) when calling the ListBuckets operation: The specified bucket does not exist: NoSuchBucket .

However, if I change the code to use localstack SQS , like so:

  ACCESS_KEY='123'
  SECRET_KEY='abc'
  sqs = boto3.client('sqs',
                      endpoint_url="http://docker.for.mac.localhost:4576",
                      use_ssl=False,
                      aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)
  print sqs.list_queues()

Everything works fine and it lists me the queues I've created in localstack SQS .

您是否在docker-compose文件或docker启动命令中仔细检查了PORTS配置,以确保您的S3端口可见?

By default the main entry point for all API invocations is (the EDGE_PORT ) 4566. I am interacting with S3 inside the localstack/localstack:latest container over that same port.

In order for it to work I also had to change the AWS SDK config to use s3ForcePathStyle: true .

Does the above work for you?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM