首页 > 解决方案 > 如何在 python 中模拟 s3 对象?

问题描述

在我的代码中,有一种方法可以从 s3 存储桶中获取 csv 文件。我需要在我的测试文件中模拟该对象。下面是源函数

s3 = boto3.client('s3')
def method():
    ...
    obj = s3.get_object(bucket, key)
    csv_content = obj['Body'].read().decode('utf-8')
    ...

我需要模拟上面的'obj'。我需要使用单独的模块吗?请提出你的想法

标签: python-3.xpython-unittestpython-unittest.mock

解决方案


Moto 是 python 中用于模拟 S3 和其他一些 AWS 服务的常用包。

https://github.com/spulec/moto


推荐阅读