Last active
February 8, 2023 11:21
-
-
Save grantcooksey/132ddc85274a50b94b821302649f9d7b to your computer and use it in GitHub Desktop.
Mocking an S3 bucket using Stubber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import json | |
import botocore.session | |
from botocore.stub import Stubber | |
from botocore.response import StreamingBody | |
expected_message = { | |
'message': 'readme' | |
} | |
encoded_message = json.dumps(expected_message).encode() | |
raw_stream = StreamingBody( | |
io.BytesIO(encoded_message), | |
len(encoded_message) | |
) | |
response = { | |
'Body': raw_stream | |
} | |
expected_params = { | |
'Bucket': 'test-bucket', | |
'Key': 'fake' | |
} | |
s3 = botocore.session.get_session().create_client('s3') | |
stubber = Stubber(s3) | |
stubber.add_response('get_object', response, expected_params) | |
stubber.activate() | |
service_response = s3.get_object(Bucket='test-bucket', Key='fake') | |
message = json.loads(service_response['Body'].read().decode('utf-8')) | |
assert message == expected_message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great, thanks so much! Much simpler than using moto (which I found to be slow).