# Buggy code
def uploadStream(self, multipart=True, allowInlining=True):
    store = self.outer
    readable_fh, writable_fh = os.pipe()
    with os.fdopen(readable_fh, "r") as readable:
        with os.fdopen(writable_fh, "w") as writable:

            def multipartReader():
                buf = readable.read(store.partSize)
                if allowInlining and len(buf) <= self._maxInlinedSize():
                    self.content = buf
                else:
                    headers = self._s3EncryptionHeaders()
                    for attempt in retry_s3():
                        with attempt:
                            upload = store.filesBucket.initiate_multipart_upload(
                                key_name=self.fileID, headers=headers
                            )
                    try:
                        for part_num in itertools.count():
                            # There must be at least one part, even if the file is empty.
                            if len(buf) == 0 and part_num > 0:
                                break
                            for attempt in retry_s3():
                                with attempt:
                                    upload.upload_part_from_file(
                                        fp=StringIO(buf),
                                        # part numbers are 1-based
                                        part_num=part_num + 1,
                                        headers=headers,
                                    )
                            if len(buf) == 0:
                                break
                            buf = readable.read(self.outer.partSize)
                    except:
                        with panic(log=log):
                            for attempt in retry_s3():
                                with attempt:
                                    upload.cancel_upload()
                    else:
                        for attempt in retry_s3():
                            with attempt:
                                self.version = upload.complete_upload().version_id

            def reader():
                buf = readable.read()
                if allowInlining and len(buf) <= self._maxInlinedSize():
                    self.content = buf
                else:
                    key = store.filesBucket.new_key(key_name=self.fileID)
                    buf = StringIO(buf)
                    headers = self._s3EncryptionHeaders()
                    for attempt in retry_s3():
                        with attempt:
                            assert buf.len == key.set_contents_from_file(
                                fp=buf, headers=headers
                            )
                    self.version = key.version_id

            thread = ExceptionalThread(target=multipartReader if multipart else reader)
            thread.start()
            yield writable
        # The writable is now closed. This will send EOF to the readable and cause that
        # thread to finish.
        thread.join()
        assert bool(self.version) == (self.content is None)


# 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 uploadStream(self, multipart=True, allowInlining=True):
    info = self
    store = self.outer

    class MultiPartPipe(WritablePipe):
        def readFrom(self, readable):
            buf = readable.read(store.partSize)
            if allowInlining and len(buf) <= info._maxInlinedSize():
                info.content = buf
            else:
                headers = info._s3EncryptionHeaders()
                for attempt in retry_s3():
                    with attempt:
                        upload = store.filesBucket.initiate_multipart_upload(
                            key_name=info.fileID, headers=headers
                        )
                try:
                    for part_num in itertools.count():
                        # There must be at least one part, even if the file is empty.
                        if len(buf) == 0 and part_num > 0:
                            break
                        for attempt in retry_s3():
                            with attempt:
                                upload.upload_part_from_file(
                                    fp=StringIO(buf),
                                    # part numbers are 1-based
                                    part_num=part_num + 1,
                                    headers=headers,
                                )
                        if len(buf) == 0:
                            break
                        buf = readable.read(info.outer.partSize)
                except:
                    with panic(log=log):
                        for attempt in retry_s3():
                            with attempt:
                                upload.cancel_upload()
                else:
                    for attempt in retry_s3():
                        with attempt:
                            info.version = upload.complete_upload().version_id

    class SinglePartPipe(WritablePipe):
        def readFrom(self, readable):
            buf = readable.read()
            if allowInlining and len(buf) <= info._maxInlinedSize():
                info.content = buf
            else:
                key = store.filesBucket.new_key(key_name=info.fileID)
                buf = StringIO(buf)
                headers = info._s3EncryptionHeaders()
                for attempt in retry_s3():
                    with attempt:
                        assert buf.len == key.set_contents_from_file(
                            fp=buf, headers=headers
                        )
                info.version = key.version_id

    with MultiPartPipe() if multipart else SinglePartPipe() as writable:
        yield writable

    assert bool(self.version) == (self.content is None)