简体   繁体   English

通过 SAM local 使用 boto3 与 Localstack S3 交互

[英]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 .我在尝试在AWS SAM local获取boto3以连接到localstack S3时遇到了一个奇怪的问题。 I know this is especially strange because SQS works fine.我知道这特别奇怪,因为SQS工作正常。 Both localstack and AWS SAM local are being invoked to run within a Docker network I've created, called test . localstackAWS SAM local都被调用以在我创建的 Docker 网络中运行,称为test

My (non-working S3) Python code is:我的(非工作 S3)Python 代码是:

  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 .上面的代码给了我错误: 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:但是,如果我将代码更改为使用localstack SQS ,如下所示:

  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 .一切正常,它列出了我在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.默认情况下,所有 API 调用的主要入口点是( EDGE_PORT )4566。我通过同一个端口与localstack/localstack:latest容器内的 S3 进行交互。

In order for it to work I also had to change the AWS SDK config to use s3ForcePathStyle: true .为了使其正常工作,我还必须更改 AWS 开发工具包配置以使用s3ForcePathStyle: true

Does the above work for you?以上对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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