import sys import boto.s3.connection #------------------------------------------------------------------------------ HOST_S3 = 'ss3.scayle.es' PORT_S3 = 443 #------------------------------------------------------------------------------ # FUNCTION: S3 Connection # DESCRIPTION: Connection to Server S3 # PARAMETERS: accessKey: Encoded token to access with AD User #------------------------------------------------------------------------------ def S3Connection (accessKey): global conn try: conn = boto.connect_s3( aws_access_key_id=accessKey, aws_secret_access_key=accessKey, host=HOST_S3, port=PORT_S3, is_secure=True, calling_format=boto.s3.connection.OrdinaryCallingFormat(), ) except: print ("AD User with access data <%s> has not privileges or does not exist." % (accessKey)) sys.exit(-1) #------------------------------------------------------------------------------ # FUNCTION: deleteKey # DESCRIPTION: Delete Key associated to bucket # PARAMETERS: bucketName Name of the bucket which contains the key # keyName Key name #------------------------------------------------------------------------------ def deleteKey (bucketName, bucketKey): bFound = False try: for bucket in conn.get_all_buckets(): if (bucket.name == bucketName): for key in bucket.list(): if (key.name == keyName): try: bucket.delete_key(key.name) bFound = True except: print ("!!! WARNING Key %s on %s bucket can not be deleted. Error %s. " % (keyName, bucket.name, (sys.exc_info()[1]))) print ("When CODE is AccessDenied the connection to OpenCayle can be done, please review your AccessKey:") print ("1) Run program s3AccessKey.py") print ("2) Run program s3CheckConnection.py") print ("If problem persists then contact to Scayle through HelpDesk tool.") sys.exit(-3) except: print ("Connection to OpenCayle can not be done, please review your AccessKey:") print ("1) Run program s3AccessKey.py") print ("2) Run program s3CheckConnection.py") print ("If problem persists then contact to Scayle through HelpDesk tool.") sys.exit (-4) if (bFound == False): print ("Bucket %s or Key %s associated to this bucket does not exist. Please review your S3 data." % (bucketName,keyName)) sys.exit(-2) ######################################################################################################### # PROGRAM ######################################################################################################### # ----------------------------- PARAMETERS EVALUATION ----------------------------- if len(sys.argv) >= 4: accessKey = str(sys.argv[1]) bucketName = str(sys.argv[2]) keyName = str (sys.argv[3]) else: print ("USE: python s3DeleteKey.py ") sys.exit(-1) # ----------------------- CONNECTION ------------------------------------------------ S3Connection (accessKey) # ----------------------- KEY CREATION ------------------------------------------- deleteKey (bucketName, keyName) # -------------- END PROCESS -------------------------------------------------------------------- print ("Key %s deleted from bucket %s successfully" % (keyName, bucketName)) sys.exit(0)