You should create a class (called something like CloudContainer
) that includes all of those global variables as members, and rewrite it as (just as a start):
class CloudContainers(object):
def __init__(self, username, key, originContainerName, targetContainerName):
self.username = username
self.key = key
self.originContainerName = originContainerName
self.targetContainerName = targetContainerName
def cloudConnect(self):
print "Creating connection"
self.connection = cloudfiles.get_connection(self.username,self.key,servicenet=True)
print "-- [DONE]"
print "Accessing containers"
self.originContainer = connection.create_container(self.originContainerName)
self.targetContainer = connection.create_container(self.targetContainerName)
print "-- [DONE]"
return
def uploadImg(self, new_name):
new_obj = self.targetContainer.create_object(new_name)
new_obj.content_type = 'image/jpeg'
new_obj.load_from_filename("up/"+new_name)
def getImg(name):
obj = self.originContainer.get_object(name)
obj.save_to_filename("down/"+name)
Thus, any function that uses these global variables (like getImg
and uploadImg
above) would be included as a method of the class.