我有使用 boto3 从 S3 存储桶下载文件的代码.
# foo.py
def dl(src_f, dest_f):
s3 = boto3.resource('s3')
s3.Bucket('mybucket').download_file(src_f, dest_f)
我现在想使用 pytest 为 dl()
编写单元测试,并使用 botocore 中可用的 stubber 模拟与 AWS 的交互。 .
@pytest.fixture
def s3_client():
yield boto3.client("s3")
from foo import dl
def test_dl(s3_client):
with Stubber(s3_client) as stubber:
params = {"Bucket": ANY, "Key": ANY}
response = {"Body": "lorem"}
stubber.add_response(SOME_OBJ, response, params)
dl('bucket_file.txt', 'tmp/bucket_file.txt')
assert os.path.isfile('tmp/bucket_file.txt')
我不确定正确的方法。如何将 bucket_file.txt
添加到 stub 响应中?我需要向什么对象add_response()
(显示为SOME_OBJ
)?
最佳答案
您是否考虑过使用 moto3 ?
您的代码看起来可能和现在一样:
# foo.py
def dl(src_f, dest_f):
s3 = boto3.resource('s3')
s3.Bucket('mybucket').download_file(src_f, dest_f)
和测试:
import boto3
import os
from moto import mock_s3
@mock_s3
def test_dl():
s3 = boto3.client('s3', region_name='us-east-1')
# We need to create the bucket since this is all in Moto's 'virtual' AWS account
s3.create_bucket(Bucket='mybucket')
s3.put_object(Bucket='mybucket', Key= 'bucket_file.txt', Body='')
dl('bucket_file.txt', 'bucket_file.txt')
assert os.path.isfile('bucket_file.txt')
代码的意图变得更加明显,因为您只是像往常一样使用 s3,除了方法调用背后没有真正的 s3。
关于python - 从 S3 下载文件的模拟 boto3 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564394/