Category: python
Upload Files from Python to AWS S3
Published on 15 Sep 2026
Explanation
An S3 bucket stores objects such as images, documents, backups, and datasets.
Code:
import boto3
s3 = boto3.client('s3')
s3.create_bucket(Bucket='my-python-bucket')
Explanation
Boto3 can upload a local file to an S3 bucket.
Code:
s3.upload_file('report.pdf', 'my-python-bucket', 'reports/report.pdf')
Explanation
Text content can be stored directly in S3 using put_object.
Code:
s3.put_object(Bucket='my-python-bucket', Key='message.txt', Body='Hello from Python')
Explanation
The download_file method copies an S3 object to the local filesystem.
Code:
s3.download_file('my-python-bucket', 'reports/report.pdf', 'report-copy.pdf')
Explanation
Python can list objects stored inside an S3 bucket.
Code:
response = s3.list_objects_v2(Bucket='my-python-bucket')
for item in response.get('Contents', []):
print(item['Key'])