# Buggy code
def _downloadStream(self, jobStoreFileID, container):
    # The reason this is not in the writer is so we catch non-existant blobs early

    blobProps = container.get_blob_properties(blob_name=jobStoreFileID)

    encrypted = strict_bool(blobProps["x-ms-meta-encrypted"])
    if encrypted and self.keyPath is None:
        raise AssertionError("Content is encrypted but no key was provided.")

    readable_fh, writable_fh = os.pipe()
    with os.fdopen(readable_fh, "r") as readable:
        with os.fdopen(writable_fh, "w") as writable:

            def writer():
                try:
                    chunkStartPos = 0
                    fileSize = int(blobProps["Content-Length"])
                    while chunkStartPos < fileSize:
                        chunkEndPos = chunkStartPos + self._maxAzureBlockBytes - 1
                        buf = container.get_blob(
                            blob_name=jobStoreFileID,
                            x_ms_range="bytes=%d-%d" % (chunkStartPos, chunkEndPos),
                        )
                        if encrypted:
                            buf = encryption.decrypt(buf, self.keyPath)
                        writable.write(buf)
                        chunkStartPos = chunkEndPos + 1
                finally:
                    # Ensure readers aren't left blocking if this thread crashes.
                    # This close() will send EOF to the reading end and ultimately cause the
                    # yield to return. It also makes the implict .close() done by the enclosing
                    # "with" context redundant but that should be ok since .close() on file
                    # objects are idempotent.
                    writable.close()

            thread = ExceptionalThread(target=writer)
            thread.start()
            yield readable
            thread.join()


# Traceback (boto.exception.S3CreateError)
ubuntu@ip-172-31-24-34:~/common-workflow-language$ toil clean aws:us-west-2:peter-cwltoil-test
2016-04-13 15:36:32,632 INFO:toil.lib.bioio: Logging set at level: INFO
2016-04-13 15:36:32,632 INFO:toil.utils.toilClean: Parsed arguments
2016-04-13 15:36:32,633 INFO:toil.utils.toilClean: Checking if we have files for toil
Traceback (most recent call last):
File "/usr/local/bin/toil", line 9, in <module>
load_entry_point('toil==3.2.0a2.dev134', '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 501, in loadOrCreateJobStore
return AWSJobStore.loadOrCreateJobStore(jobStoreArgs, config=config)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/aws/jobStore.py", line 117, in loadOrCreateJobStore
return cls(region, namePrefix, config=config, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/aws/jobStore.py", line 174, in __init__
self.filesBucket = self._getOrCreateBucket(qualify('files'), versioning=True)
File "/usr/local/lib/python2.7/dist-packages/toil/jobStores/aws/jobStore.py", line 469, in _getOrCreateBucket
bucket = self.s3.create_bucket(bucket_name, location=self.region)
File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 616, in create_bucket
response.status, response.reason, body)
boto.exception.S3CreateError: S3CreateError: 409 Conflict
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>BucketAlreadyExists</Code><Message>The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.</Message><BucketName>peter-cwltoil-test--files</BucketName><RequestId>DAF5D5E6B138F1AB</RequestId><HostId>t9Fz0/fKNcxA5q7gLBuk7FMu9AxCD4AdyXMIWhWOtJVyNS8ARDxnWDQOBZdeTmpkcVtXa9brMss=</HostId></Error>

# Fixed code
def _downloadStream(self, jobStoreFileID, container):
    # The reason this is not in the writer is so we catch non-existant blobs early

    blobProps = container.get_blob_properties(blob_name=jobStoreFileID)

    encrypted = strict_bool(blobProps["x-ms-meta-encrypted"])
    if encrypted and self.keyPath is None:
        raise AssertionError("Content is encrypted but no key was provided.")

    outer_self = self

    class DownloadPipe(ReadablePipe):
        def writeTo(self, writable):
            chunkStart = 0
            fileSize = int(blobProps["Content-Length"])
            while chunkStart < fileSize:
                chunkEnd = chunkStart + outer_self._maxAzureBlockBytes - 1
                buf = container.get_blob(
                    blob_name=jobStoreFileID,
                    x_ms_range="bytes=%d-%d" % (chunkStart, chunkEnd),
                )
                if encrypted:
                    buf = encryption.decrypt(buf, outer_self.keyPath)
                writable.write(buf)
                chunkStart = chunkEnd + 1

    with DownloadPipe() as readable:
        yield readable