我想從 S3 存盤桶上的特定目錄中獲取所有檔案,如下所示:
def get_files_from_s3(bucket_name, s3_prefix):
files = []
s3_resource = boto3.resource("s3")
bucket = s3_resource.Bucket(bucket_name)
response = bucket.objects.filter(Prefix=s3_prefix)
for obj in response:
if obj.key.endswidth('.zip'):
# get all archives
files.append(obj.key)
return files
我的問題是關于測驗它;因為我想模擬 中的物件串列response以便能夠對其進行迭代。這是我嘗試過的:
from unittest.mock import patch
from dataclasses import dataclass
@dataclass
class MockZip:
key = 'file.zip'
@patch('module.boto3')
def test_get_files_from_s3(self, mock_boto3):
bucket = mock_boto3.resource('s3').Bucket(self.bucket_name)
response = bucket.objects.filter(Prefix=S3_PREFIX)
response.return_value = [MockZip()]
files = module.get_files_from_s3(BUCKET_NAME, S3_PREFIX)
self.assertEqual(['file.zip'], files)
我收到這樣的斷言錯誤: E AssertionError: ['file.zip'] != []
有沒有人有更好的方法?我使用了 struct 但我不認為這是問題所在,我想我得到了一個空串列,因為response它不可迭代。那么我怎樣才能將它模擬為模擬物件串列而不僅僅是一個MockMagick型別呢?
謝謝
uj5u.com熱心網友回復:
您可以使用moto,這是一個專門用于模擬 boto3 呼叫的開源庫。它允許您直接使用 boto3,而不必擔心手動設定模擬。
您當前使用的測驗函式如下所示:
from moto import mock_s3
@pytest.fixture(scope='function')
def aws_credentials():
"""Mocked AWS Credentials, to ensure we're not touching AWS directly"""
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'
@mock_s3
def test_get_files_from_s3(self, aws_credentials):
s3 = boto3.resource('s3')
bucket = s3.Bucket(self.bucket_name)
# Create the bucket first, as we're interacting with an empty mocked 'AWS account'
bucket.create()
# Create some example files that are representative of what the S3 bucket would look like in production
client = boto3.client('s3', region_name='us-east-1')
client.put_object(Bucket=self.bucket_name, Key="file.zip", Body="...")
client.put_object(Bucket=self.bucket_name, Key="file.nonzip", Body="...")
# Retrieve the files again using whatever logic
files = module.get_files_from_s3(BUCKET_NAME, S3_PREFIX)
self.assertEqual(['file.zip'], files)
Moto 的完整檔案可以在這里找到:http : //docs.getmoto.org/en/latest/index.html
免責宣告:我是 Moto 的維護者。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/374056.html
標籤:Python 单元测试 亚马逊-s3 嘲笑 boto3
上一篇:將字典內容寫入以鍵為檔案名的檔案
下一篇:Sagemaker檔案大小限制?
