我正在为一个使用boto3从s3中查找/获取密钥的快速类编写测试用例。我过去曾使用moto测试boto(不是3)代码,但现在正试图通过这个项目转移到boto3,遇到了一个问题:
class TestS3Actor(unittest.TestCase):
@mock_s3
def setUp(self):
self.bucket_name = 'test_bucket_01'
self.key_name = 'stats_com/fake_fake/test.json'
self.key_contents = 'This is test data.'
s3 = boto3.session.Session().resource('s3')
s3.create_bucket(Bucket=self.bucket_name)
s3.Object(self.bucket_name, self.key_name).put(Body=self.key_contents)
错误:
...
File "/Library/Python/2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 344, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
File "/Library/Python/2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 314, in _raise_timeout
if 'timed out' in str(err) or 'did not complete (read)' in str(err): # Python 2.6
TypeError: __str__ returned non-string (type WantWriteError)
botocore.hooks: DEBUG: Event needs-retry.s3.CreateBucket: calling handler <botocore.retryhandler.RetryHandler object at 0x10ce75310>
看起来moto没有正确地模拟boto3调用-我该如何使其工作?
对我有用的是,在用
boto3
运行模拟测试之前,先用boto
设置环境。下面是一个工作片段:
当使用
py.test test.py
运行时,将得到以下输出:根据this信息,似乎还不支持使用boto3sput将流式上传到s3。
在我的例子中,我使用以下方法成功地将对象上载到bucket:
其中“file_to_upload”是要上载到s3 bucket的本地文件。对于您的测试用例,您只需创建一个临时文件来检查此功能:
相关问题 更多 >
编程相关推荐