using local endpoint with boto2

对着背影说爱祢 提交于 2020-01-05 04:12:12

问题


I am trying to mock AWS s3 api calls using boto2. I create local s3 endpoint using localstack and can use this using boto3 easily as below,

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

But I did not find way to do this using boto2. Is there any way preferably using ~/.boto or ~/.aws/config?

Tried providing endpoint with boto2 but it failed.

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')

error:

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

I am looking to use local endpoints for all AWS services for testing purpose.


回答1:


This works for me:

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')

You need to set the connection_cls attribute wish is NoneType by default.



来源:https://stackoverflow.com/questions/45757021/using-local-endpoint-with-boto2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!