CURD Operation on Amazon S3 Bucket
Chandan Kumar Singh
Upcoming SE @TCS | Ex-President & Founder @ZERO ONE Coding Club | Ex-DRDO Software Developer Intern
Introduction
Amazon Simple Storage Service (S3) is one such prominent cloud storage service offered by Amazon Web Services (AWS). In this article, we will explore how to streamline your AWS S3 operations using the Python programming language and the Boto3 library. We'll cover the creation, uploading, deletion of files, as well as the creation and deletion of S3 buckets.
Step-by-Step explanations
Getting Started: Setting Up Boto3 and AWS Credentials
pip install boto3
import boto3
Creating an S3 Bucket
Python Code
def create_s3_bucket(bucket_name, region='us-east-1'):
? ? try:
? ? ? ? s3 = boto3.client('s3', region_name=region)
? ? ? ? s3.create_bucket(Bucket=bucket_name)
? ? ? ? print(f"S3 bucket '{bucket_name}' created successfully in region '{region}'.")
? ? ? ? return bucket_name
? ? except Exception as e:
? ? ? ? print(f"An error occurred while creating the S3 bucket: {e}")
? ? ? ? return None
bucket_name = input("your-bucket-name")
region = 'us-east-1'
created_bucket = create_s3_bucket(bucket_name, region)
Output
Uploading a File to an S3 Bucket
Python Code
def upload_file_to_s3(file_path, bucket_name, s3_key):
? ? try:
? ? ? ? s3 = boto3.client('s3')
? ? ? ? s3.upload_file(file_path, bucket_name, s3_key)
? ? ? ? print(f"File '{file_path}' uploaded to S3 bucket '{bucket_name}' with key '{s3_key}'.")
? ? ? ? return True
? ? except Exception as e:
? ? ? ? print(f"An error occurred while uploading the file: {e}")
? ? ? ? return False
bucket_name = input('your-bucket-name:')
local_file_path = input('Path of local file:')
s3_file_key = input('File name:')
upload_file_to_s3(local_file_path, bucket_name, s3_file_key)
Output
领英推荐
Deleting a File from an S3 Bucket
Python Code
def delete_file_from_s3(bucket_name, s3_key):
? ? try:
? ? ? ? s3 = boto3.client('s3')
? ? ? ? s3.delete_object(Bucket=bucket_name, Key=s3_key)
? ? ? ? print(f"File '{s3_key}' deleted from S3 bucket '{bucket_name}'.")
? ? ? ? return True
? ? except Exception as e:
? ? ? ? print(f"An error occurred while deleting the file: {e}")
? ? ? ? return False
bucket_name = input('your-bucket-name')
s3_file_key = input('Enter file-key name')
delete_file_from_s3(bucket_name, s3_file_key)
Output
Deleting an S3 Bucket
Python Code
def delete_s3_bucket(bucket_name):
? ? try:
? ? ? ? s3 = boto3.client('s3')
? ? ? ? s3.delete_bucket(Bucket=bucket_name)
? ? ? ? print(f"S3 bucket '{bucket_name}' deleted successfully.")
? ? ? ? return True
? ? except Exception as e:
? ? ? ? print(f"An error occurred while deleting the S3 bucket: {e}")
? ? ? ? return False
bucket_name = input('your-bucket-name')
delete_s3_bucket(bucket_name)
Output
Conclusion
Feedback/Queries
I hope this article will be useful for you and that you learned something new Please, feel free to drop any questions in the comments below. I would be happy to answer them.
Thanks for reading???
C.Sc. Student || Devops || Data Analysis || Python || C++ || JAVA || BRLPS CBO's Accountant, Bihar Govt.|| Technical Writer
1 年Bahut mehnat kar rahe Hain aap log ????????