Created
August 25, 2014 19:18
-
-
Save chrisseto/c674e84540acc8178578 to your computer and use it in GitHub Desktop.
Streaming tornado uploads to Amazon S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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