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: deleteBucket # DESCRIPTION: Bucket Delete # PARAMETERS: bucketName Name associated to the bucket to delete #------------------------------------------------------------------------------ def deleteBucket (bucketName): bFound = False try: for bucket in conn.get_all_buckets(): if (bucketName == bucket.name): # --- Delete keys associated to bucket for key in bucket.list(): try: bucket.delete_key(key.name) except: print ("!!! WARNING Bucket %s Key %s can not be deleted. Error %s. " % (bucket.name,key.name, (sys.exc_info()[1]))) sys.exit(-4) # --- Delete buckets try: conn.delete_bucket(bucket.name) bFound = True except: print ("Bucket %s can not be deleted. Review the following message. Please attention to field CODE if exist." %s (bucketName)) print (sys.exc_info()[1]) print ("If CODE is AccessDenied then the 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(-2) 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 does not exist. Please review your S3 data" % (bucketName)) sys.exit(-3) ######################################################################################################### # PROGRAM ######################################################################################################### # ----------------------------- PARAMETERS EVALUATION ----------------------------- if len(sys.argv) >= 3: accessKey = str(sys.argv[1]) bucketName = str(sys.argv[2]) else: print ("USE: python s3DeleteBucket.py ") sys.exit(-1) # ----------------------- CONECTION ------------------------------------------------ S3Connection (accessKey) # -------- CREAR BUCKETS EN BASE A PARAMETROS -------------------------------------- deleteBucket (bucketName) # -------------- END PROCESS ------------------------------------------- print ("Bucket %s deleted sucessfully" % (bucketName)) sys.exit(0)