s3 urls – get bucket name and path

Since it’s just a normal URL, you can use urlparse to get all the parts of the URL. >>> from urlparse import urlparse >>> o = urlparse(‘s3://bucket_name/folder1/folder2/file1.json’, allow_fragments=False) >>> o ParseResult(scheme=”s3″, netloc=”bucket_name”, path=”/folder1/folder2/file1.json”, params=””, query=”, fragment=””) >>> o.netloc ‘bucket_name’ >>> o.path ‘/folder1/folder2/file1.json’ You may have to remove the beginning slash from the key as the … Read more

Mocking boto3 S3 client method Python

Botocore has a client stubber you can use for just this purpose: docs. Here’s an example of putting an error in: import boto3 from botocore.stub import Stubber client = boto3.client(‘s3’) stubber = Stubber(client) stubber.add_client_error(‘upload_part_copy’) stubber.activate() # Will raise a ClientError client.upload_part_copy() Here’s an example of putting a normal response in. Additionally, the stubber can now … Read more