A Python implementation for basic AWS S3 operations using boto3. This implementation provides functions for listing, creating, uploading, downloading, and deleting S3 bucket contents.
- Python 3.8.19
- boto3 (AWS SDK for Python)
- AWS Account with S3 access
- AWS Access Key and Secret Access Key
Install the required AWS SDK for Python:
pip install boto3
You'll need to configure the following AWS credentials:
aws_accesskey = <Your Access Key>
aws_secretaccess = <Your secret access key>
myregion = <your region>
mybucket = <your bucket name>
import boto3
import os
import logging
Lists all S3 buckets in your AWS account.
List_My_Buckets(aws_access, aws_secret, aws_region)
Creates a new S3 bucket with location constraint.
create_bucket(aws_access, aws_secret, aws_region, bucket_name)
Uploads a file to specified S3 bucket.
upload_file(aws_access, aws_secret, aws_region, bucket_name, file_toupload, object_name=None)
- If
object_name
is not specified, the filename will be used
Two methods are provided for downloading files:
Method 1 (using download_fileobj):
download_file(aws_access, aws_secret, aws_region, bucket_name, file_todownload, object_name)
Method 2 (using download_file):
download_file_2(aws_access, aws_secret, aws_region, bucket_name, file_todownload, object_name)
Lists all objects in a specified bucket with their sizes in KB.
list_object_v2(aws_access, aws_secret, aws_region, bucket_name)
Deletes a specific file from a bucket.
delete_object(aws_access, aws_secret, aws_region, bucket_name, file_todelete)
All functions include error handling using try-except blocks for ClientError exceptions and logging.