Created
March 16, 2011 17:55
-
-
Save BradWhittington/872948 to your computer and use it in GitHub Desktop.
Buffered, file-like class to wrap amazon S3 / boto multipart upload
This file contains hidden or 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
from StringIO import StringIO | |
import boto.s3 | |
from django.conf import settings | |
class MultiPartUploader: | |
counter = 0 | |
def __init__(self, bucket_name, key_name, buf_size=32768): | |
self.connection = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY) | |
self.bucket = self.connection.get_bucket(bucket_name) | |
self.mp = self.bucket.initiate_multipart_upload(key_name) | |
self.buf_size = buf_size | |
self.buffer = StringIO() | |
def write(self,s): | |
self.buffer.write(s) | |
if self.buffer.len > self.buf_size: | |
self.flush() | |
def flush(self): | |
if self.buffer.len: | |
self.counter+=1 | |
self.mp.upload_part_from_file(self.buffer, self.counter) | |
self.buffer.close() | |
self.buffer = StringIO() | |
def close(self): | |
self.mp.flush() | |
self.mp.complete_upload() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage is:
fd.close() is critical and has to happen, or the upload will need to manually be set to 'complete'