使用boto2的本地端点

问题描述 投票:1回答:1

我正在尝试使用boto2模拟AWS s3 api调用。我使用localstack创建本地s3端点,并且可以使用boto3轻松使用它,如下所示,

import boto3
s3_client = boto3.client('s3', endpoint_url='http://localhost:4572')
bucket_name = 'my-bucket'
s3_client.create_bucket(Bucket=bucket_name)

但我没有找到使用boto2做到这一点的方法。有没有办法最好使用〜/ .boto或〜/ .aws / config?

尝试使用boto2提供端点但它失败了。

import boto
boto.s3.S3RegionInfo(name='test-s3-region', endpoint='http://127.0.0.1:4572/')
s3 = boto.s3.connect_to_region('test-s3-region')
print s3.get_bucket('test-poc')

错误:

AttributeError: 'NoneType' object has no attribute 'get_bucket'

我希望将所有AWS服务的本地端点用于测试目的。

python amazon-s3 boto atlassian-localstack
1个回答
0
投票

这对我有用:

import boto
from boto.s3.connection import S3Connection
region = boto.s3.S3RegionInfo(name='test-s3-region', endpoint='http://127.0.0.1:4572/', connection_cls=S3Connection)
conn = region.connect()
print conn.get_bucket('test-poc')

你需要设置connection_cls属性wish is NoneType by default

© www.soinside.com 2019 - 2024. All rights reserved.