« Back to Index

[Python Boto3 S3 Example]

View original Gist on GitHub

Tags: #python3 #boto3 #aws #s3

Python Boto3 S3 Example.py

import boto3

aws_access_key_id = "123"
aws_secret_access_key = "456"

session = boto3.session.Session(
    aws_access_key_id=aws_access_key_id,
    aws_secret_access_key=aws_secret_access_key,
)

s3_resource = session.resource("s3")
s3_obj = s3_resource.Object("foo", "bar/baz")
s3_obj.get()

"""
RESPONSE...

{'ResponseMetadata': {'RequestId': '123',
  'HostId': '456',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'x-amz-id-2': '789',
   'x-amz-request-id': '123',
   'date': 'Wed, 05 Feb 2020 19:03:29 GMT',
   'x-amz-replication-status': 'COMPLETED',
   'last-modified': 'Wed, 27 Sep 2017 18:45:35 GMT',
   'etag': '"456"',
   'x-amz-storage-class': 'STANDARD_IA',
   'x-amz-version-id': '789',
   'accept-ranges': 'bytes',
   'content-type': 'text/html',
   'content-length': '283986',
   'server': 'AmazonS3'},
  'RetryAttempts': 0},
 'AcceptRanges': 'bytes',
 'LastModified': datetime.datetime(2017, 9, 27, 18, 45, 35, tzinfo=tzutc()),
 'ContentLength': 283986,
 'ETag': '"123"',
 'VersionId': '456',
 'ContentType': 'text/html',
 'Metadata': {},
 'StorageClass': 'STANDARD_IA',
 'ReplicationStatus': 'COMPLETED',
 'Body': <botocore.response.StreamingBody at 0x111f9a9d0>}
"""