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"])
boto3.client. That's a strong indication of a bad abstraction.s3_boto3_methodsis trying to both act as aboto3.clientfactory and act as a business-logic wrapper around aboto3.client. For the latter purpose, it doesn't need to know about any of those values, it just needs to have aboto3.clientinstance that it can use. I'd recommend redesigning your class to accept aboto3.clientinstance in its__init__and store that object as an attribute (see also "dependency injection").