python - 从 S3 下载文件的模拟 boto3 响应

标签 python amazon-web-services amazon-s3 boto3 botocore

我有使用 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/

相关文章:

python - 如何预先确定为 N 个项目创建哈希表的理想大小?

amazon-web-services - AWS - 通过 CloudFormation 覆盖 AMI 中的 DeleteOnTermination

amazon-web-services - 如何在 AWS Glue/Athena 上使用 AVRO 格式

amazon-web-services - CodeBuild 构建的 docker 构建阶段出错

python - 从外部 url 导入 python 模块

amazon-s3 - Spark 上的 saveAsTextFile 到 s3 不起作用,只是挂起

python - 使用 get_or_create 是否正确?

Python 和最多 21 个字符的随机键

python - <> 在 beautifulsoup 中变成 &lt &gt

amazon-web-services - 现有 VPC 中 s3 的 cloudformation 模板