def bucketExists()

in scripts/yapl/S3Helper.py [0:0]


  def bucketExists(self, bucketName):
    """
      Return True if the bucket exists and access is permitted.
      If bucket does not exist return False
      If bucket exists but access is forbidden, raise an exception.
      
      Picked this up from:
      https://stackoverflow.com/questions/26871884/how-can-i-easily-determine-if-a-boto-3-s3-bucket-resource-exists
    """
    methodName = "bucketExists"
    
    result = False
    try:
      self.s3Client.head_bucket(Bucket=bucketName)
      
      result = True
    except ClientError as e:
      # If a client error is thrown, then check that it was a 404 error.
      error_code = e.response['Error']['Code']
      if (TR.isLoggable(Level.FINEST)):
        TR.finest(methodName,"Error code: %s" % error_code)
      #endIf
      error_code = int(error_code)
      if (error_code == 404):
        result = False
      else:
        if (error_code == 403):
          raise AccessDeniedException("Access denied to S3 bucket named: %s" % bucketName)
        else: 
          raise e
        #endIf
      #endIf
    #endTry
    
    return result