Hello All Python programmers welcome to the world of Python AWS and Boto3.

I was working with Python and aws recently. My code was having lots of interaction with S3 buckets and objects . And I then thought its the perfect time to write a class where I can pull all these as separate functions but still be in same class . So i wrote one class with couple of functions in it that does various activities with the AWS S3 bucket. The package I used is Boto3 . I have no idea if we have any alternative to Boto3.

My sample Python class using Boto3 and AWS is below

class AWS_Helper:

    def __init__(self):
        self.s3_client= None
        self.region_name = <region name>
        session  = boto3.Session()
        self.s3_client = session.resource('s3')
        print("Connected")

    def create_bucket(self, bucketname):
        print("Creating Bucket:"+bucketname)
        self.s3_client.create_bucket(Bucket=bucketname,CreateBucketConfiguration={
                              'LocationConstraint': self.region_name})
        print("Created")

    def get_all_buckets(self):
        return self.s3_client.buckets.all()

    def get_all_bucket_objects(self):
        my_bucket = self.s3_client.Bucket(self.bucketname)
        res = []
        # for s3_file in my_bucket.objects.all():
        for s3_file in sorted(my_bucket.objects.all(), key=obj_last_modified, reverse=True):
            res.append({"key":s3_file.key,"date":s3_file.last_modified,"size":s3_file.size})
        return res

    def check_bucket_exists(self, bucketname):
        return self.s3_client.Bucket(bucketname) in self.get_all_buckets()

    def put_file_to_bucket(self, filepath,filename):
        #data = open(filepath, 'rb')
        self.s3_client.Bucket(self.bucketname).upload_file(filepath, filename)

    def delete_file(self,object_name):
        try:
            obj = self.s3_client.Object(self.bucketname, object_name)
            obj.delete()
        except Exception as e:
            raise Exception(str(e))
        return True

About the functions

  • create_bucket : Helps you to create a bucket.
  • get_all_buckets : Helps to get the list of all buckets
  • get_all_bucket_objects : Helps to get the list of all objects in a given bucket.
  • check_bucket_exists : This method will help you to check if bucket exists or not.
  • put_file_to_bucket : To add an object to your bucket use this method.
  • delete_file : Use this method to delete a file from a bucket.

Requirements to get this running

  • Requires the installation of the “boto3” Python package: “pip install boto3”. You can find more information about boto3 here.
  • Might need to set your region name . Replace “<region name>” with your value
  • Note: To establish a connection to S3, you might need to set AWS credentials for the class.

What is AWS Boto3 ?

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to write software that makes use of services like Amazon S3 and Amazon EC2. Our doc site contains the latest and most up-to-date documentation, including a list of supported services.

Voila !

Please use this class and see if its useful to you. I am using this class from few months now and it works. You might need to customise the class or methods to fit your need.

Have a happy programming 🙂

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *