def _getKeyForUrl(url, existing=None):
        """
        Extracts a key from a given s3:// URL. On return, but not on exceptions, this method
        leaks an S3Connection object. The caller is responsible to close that by calling
        key.bucket.connection.close().

        :param bool existing: If True, key is expected to exist. If False, key is expected not to
               exists and it will be created. If None, the key will be created if it doesn't exist.

        :rtype: Key
        """
        # Get the bucket's region to avoid a redirect per request
        try:
            with closing(boto.connect_s3()) as s3:
                location = s3.get_bucket(url.netloc).get_location()
                region = bucket_location_to_region(location)
        except S3ResponseError as e:
            if e.error_code == 'AccessDenied':
                s3 = boto.connect_s3()
            else:
                raise
        else:
            # Note that caller is responsible for closing the connection
            s3 = boto.s3.connect_to_region(region)

        try:
            keyName = url.path[1:]
            bucketName = url.netloc
            bucket = s3.get_bucket(bucketName)
            key = bucket.get_key(keyName.encode('utf-8'))
            if existing is True:
                if key is None:
                    raise RuntimeError("Key '%s' does not exist in bucket '%s'." %
                                       (keyName, bucketName))
            elif existing is False:
                if key is not None:
                    raise RuntimeError("Key '%s' exists in bucket '%s'." %
                                       (keyName, bucketName))
            elif existing is None:
                pass
            else:
                assert False
            if key is None:
                key = bucket.new_key(keyName)
        except:
            with panic():
                s3.close()
        else:
            return key
def _getKeyForUrl ( url , existing = None ) : try : with closing ( boto . connect_s3 ( ) ) as s3 : location = s3 . get_bucket ( url . netloc ) . get_location ( ) region = bucket_location_to_region ( location ) except S3ResponseError as e : if e . error_code == 'AccessDenied' : s3 = boto . connect_s3 ( ) else : raise else : s3 = boto . s3 . connect_to_region ( region ) try : keyName = url . path [ 1 : ] bucketName = url . netloc bucket = s3 . get_bucket ( bucketName ) key = bucket . get_key ( keyName . encode ( 'utf-8' ) ) if existing is True : if key is None : raise RuntimeError ( "Key '%s' does not exist in bucket '%s'." % ( keyName , bucketName ) ) elif existing is False : if key is not None : raise RuntimeError ( "Key '%s' exists in bucket '%s'." % ( keyName , bucketName ) ) elif existing is None : pass else : assert False if key is None : key = bucket . new_key ( keyName ) except : with panic ( ) : s3 . close ( ) else : return key
Extracts a key from a given s3:// URL. On return, but not on exceptions, this method
        leaks an S3Connection object. The caller is responsible to close that by calling
        key.bucket.connection.close().

        :param bool existing: If True, key is expected to exist. If False, key is expected not to
               exists and it will be created. If None, the key will be created if it doesn't exist.

        :rtype: Key