# Buggy code
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:
            region = bucket_location_to_region(s3.get_bucket(url.netloc).get_location())
    except S3ResponseError as e:
        if e.error_code == "AccessDenied":
            log.warn(
                "Could not determine location of bucket hosting URL '%s', reverting "
                "to generic S3 endpoint.",
                url.geturl(),
            )
            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)
        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


# Traceback (toil.jobStores.abstractJobStore.NoSuchFileException)
cmarkello@c1master1:~$ toil clean azure:hgvm:hgvmevaljobstore
2016-05-13 23:08:36,438 INFO:toil.lib.bioio: Logging set at level: INFO
2016-05-13 23:08:36,445 INFO:toil.utils.toilClean: Parsed arguments
2016-05-13 23:08:36,445 INFO:toil.utils.toilClean: Checking if we have files for toil
2016-05-13 23:08:36,525 INFO:requests.packages.urllib3.connectionpool: Starting new HTTPS connection (1): hgvm.table.core.windows.net
2016-05-13 23:08:36,724 INFO:requests.packages.urllib3.connectionpool: Starting new HTTPS connection (1): hgvm.blob.core.windows.net
Traceback (most recent call last):
File "/usr/local/bin/toil", line 9, in <module>
load_entry_point('toil==3.2.0a2', 'console_scripts', 'toil')()
File "/usr/local/lib/python2.7/dist-packages/toil/utils/toilMain.py", line 30, in main
module.main()
File "/usr/local/lib/python2.7/dist-packages/toil/utils/toilClean.py", line 53, in main
jobStore = Toil.loadOrCreateJobStore(options.jobStore)
File "/usr/local/lib/python2.7/dist-packages/toil/common.py", line 589, in loadOrCreateJobStore
return AzureJobStore(account, namePrefix, config=config)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 120, in __init__
super(AzureJobStore, self).__init__(config=config)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/abstractJobStore.py", line 141, in __init__
with self.readSharedFileStream("config.pickle") as fileHandle:
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/azureJobStore.py", line 318, in readSharedFileStream
raise NoSuchFileException(sharedFileID)
toil.jobStores.abstractJobStore.NoSuchFileException: File 'a77581f6_ad82_5142_acb7_5a6b01eab3fb' does not exist

# Fixed code
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":
            log.warn(
                "Could not determine location of bucket hosting URL '%s', reverting "
                "to generic S3 endpoint.",
                url.geturl(),
            )
            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)
        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