Skip to content

Instantly share code, notes, and snippets.

@chrisseto
Created August 25, 2014 19:18
Show Gist options
  • Save chrisseto/c674e84540acc8178578 to your computer and use it in GitHub Desktop.
Save chrisseto/c674e84540acc8178578 to your computer and use it in GitHub Desktop.
Streaming tornado uploads to Amazon S3
@web.stream_request_body
class UploadHandler(RequestHandler):
def prepare(self):
self.count = 0
self.buffer_len = 0
self.buffer = StringIO()
self.mp = bucket.initiate_multipart_upload('Multiparts are cool')
def data_received(self, data):
self.buffer.write(data)
self.buffer_len+=len(data)
if self.buffer_len >= 5 * 1024**2:
self.flush_buffer()
def flush_buffer(self):
self.buffer.seek(0)
self.count += 1
self.mp.upload_part_from_file(self.buffer, self.count)
self.buffer = StringIO()
self.buffer_len = 0
def post(self):
if self.count == 0:
self.buffer.seek(0)
key = bucket.new_key('Singles are cool too')
key.set_contents_from_file(self.buffer)
else:
if self.buffer_len != 0:
self.flush_buffer()
complete = self.mp.complete_upload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment