Skip to content

Instantly share code, notes, and snippets.

@amarjandu
Created April 28, 2021 16:45
Show Gist options
  • Save amarjandu/334ffd668dd849991ba9949a32dcc3b8 to your computer and use it in GitHub Desktop.
Save amarjandu/334ffd668dd849991ba9949a32dcc3b8 to your computer and use it in GitHub Desktop.
Moto does not check for syntatically correct but invalid version ids
import os
from uuid import (
uuid4
)
import boto3
from moto import (
mock_s3
)
@mock_s3
def main():
s_client = boto3.client('s3')
bucket = 'amar-test-delete'
file_name = 'junk_file'
resp = s_client.create_bucket(Bucket=bucket)
print(resp)
resp = s_client.put_bucket_versioning(Bucket=bucket,
VersioningConfiguration={
'Status': 'Enabled'
})
print(resp)
resp = s_client.put_object(Bucket=bucket,
Key=file_name,
Body=os.urandom(200),
ContentType='application/octet-stream')
print(resp)
new_version = resp['VersionId']
print(f'VersionID is {new_version}')
for version in (new_version, str(uuid4())):
try:
print(f'Attempting with version: {version}')
resp = s_client.get_object(Bucket=bucket,
Key=file_name,
VersionId=version)
except s_client.exceptions.NoSuchKey:
print('No Key Found')
print(resp)
else:
print(resp)
if __name__ == '__main__':
main()
"""
{'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {}, 'RetryAttempts': 0}}
{'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {}, 'RetryAttempts': 0}}
{'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'etag': '"f7d81c1d467c32b3de121d83c43b5163"', 'last-modified': 'Wed, 28 Apr 2021 16:43:45 GMT', 'content-length': '200', 'x-amz-version-id': 'd97e724c-ea46-466a-a64b-5805588af3fc'}, 'RetryAttempts': 0}, 'ETag': '"f7d81c1d467c32b3de121d83c43b5163"', 'VersionId': 'd97e724c-ea46-466a-a64b-5805588af3fc'}
VersionID is d97e724c-ea46-466a-a64b-5805588af3fc
Attempting with version: d97e724c-ea46-466a-a64b-5805588af3fc
{'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'content-type': 'application/octet-stream', 'content-md5': '99gcHUZ8MrPeEh2DxDtRYw==', 'etag': '"f7d81c1d467c32b3de121d83c43b5163"', 'last-modified': 'Wed, 28 Apr 2021 16:43:45 GMT', 'content-length': '200', 'x-amz-version-id': 'd97e724c-ea46-466a-a64b-5805588af3fc'}, 'RetryAttempts': 0}, 'LastModified': datetime.datetime(2021, 4, 28, 16, 43, 45, tzinfo=tzutc()), 'ContentLength': 200, 'ETag': '"f7d81c1d467c32b3de121d83c43b5163"', 'VersionId': 'd97e724c-ea46-466a-a64b-5805588af3fc', 'ContentType': 'application/octet-stream', 'Metadata': {}, 'Body': <botocore.response.StreamingBody object at 0x111057370>}
Attempting with version: 5b6a5a42-0a67-4101-ba7c-053e77b0d12d
Traceback (most recent call last):
File "/Users/amar/dev/hca/azul/moto_bucket_version.py", line 49, in <module>
main()
File "/Users/amar/dev/hca/azul/.venv/lib/python3.8/site-packages/moto/core/models.py", line 106, in wrapper
result = func(*args, **kwargs)
File "/Users/amar/dev/hca/azul/moto_bucket_version.py", line 38, in main
resp = s_client.get_object(Bucket=bucket,
File "/Users/amar/dev/hca/azul/.venv/lib/python3.8/site-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/amar/dev/hca/azul/.venv/lib/python3.8/site-packages/botocore/client.py", line 676, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InvalidArgument) when calling the GetObject operation: Invalid version id specified
Process finished with exit code 1
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment