我想对从 aws 导入的文件进行测试。我使用 moto 模拟 s3,以免弄乱实际数据。但是,现在 aws 似乎是空的,因此我决定在 mocked s3 上上传一些测试文件。我该怎么做?
这是我的设置,
Conftest.py:
@pytest.fixture(scope='function')
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
os.environ['AWS_SECURITY_TOKEN'] = 'testing'
os.environ['AWS_SESSION_TOKEN'] = 'testing'
@pytest.fixture(scope='function')
def s3(aws_credentials):
with mock_s3():
yield boto3.client('s3', region_name='us-east-1')
测试文件:
class TestLoadRecommendations:
@pytest.fixture(autouse=True)
def _setup(self, s3):
self.bucket = s3.create_bucket(Bucket=settings.SERVER_DATA_S3_BUCKET)
self.bucket.upload_file("tmp/recommendations-2021-04-13T17_28_06.csv", "tmp/recommendations-2021-04-13T17_28_06.csv")
但是,上传它会引发错误TypeError: expected string or bytes-like object
,但我确信我使用了不正确的命令进行文件上传。有人可以帮忙吗?谢谢!