0

I am using boto3 library to establish connection to an s3 compatible storage. I had created a class with constructor that take cluster details, access key & IDs as inputs, having these initiated. now created a method that opens s3 clients to the clusters and returned the s3 client object. want to use this in a diff method of the same class to list buckets:

class s3_boto3_methods():

    def __init__(self,host,port,user,accesskey,session_token = None):
        #self.host = host
        self.port = port
        self.host = host + ":" + str(self.port)
        self.user_key = user
        self.access_key = accesskey
        session_token = None

   def s3_Client_Connection(self):
        s3_client = boto3.client('s3',use_ssl=False,verify=False, aws_access_key_id=self.user_key,aws_secret_access_key=self.access_key,endpoint_url=self.host)
         return (s3_client)
   
   def listBuckets(self,client):
         response = client.list_buckets()['Buckets'] 
         print ("Buckets for user:",self.user_key)
         for item in response:
             print (item["Name"])

if __name__ == "__main__":
    instance=s3_boto3_methods("host","port","access_id","access_key")
    s3_client=instance.s3_Client_Connection()
    instance.listBuckets(s3_client)

In the above, the method s3_Connect_Connection returns s3_client object, i need to use this inside listBuckets method. Currently, i am handling this by calling instance.s3_Client_Connection and passing this returned connection as parameter in next line.

Open to suggestions, that can improve this code snippet.

Other Option is to call s3_client_connection inside the listBuckets, so whenever list buckets is called, it sets up client connection first:

    def listBuckets(self):
    s3_client=self.s3_Client_Connection()
    response = s3_client.list_buckets()['Buckets'] #here response is a list with dictionaries as list items in it.
    print ("Buckets for user:",self.user_key)
    for item in response:
        print (item["Name"])
2
  • In terms of design, this probably shouldn't be a class to begin with. Or at very least, it should be broken up to have a clearer singular responsibility. Commented Oct 20, 2023 at 16:12
  • You have a lot of attributes that seem to only exist to later be passed to boto3.client. That's a strong indication of a bad abstraction. s3_boto3_methods is trying to both act as a boto3.client factory and act as a business-logic wrapper around a boto3.client. For the latter purpose, it doesn't need to know about any of those values, it just needs to have a boto3.client instance that it can use. I'd recommend redesigning your class to accept a boto3.client instance in its __init__ and store that object as an attribute (see also "dependency injection"). Commented Oct 20, 2023 at 16:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.