Created
August 15, 2013 15:20
-
-
Save bootandy/6241703 to your computer and use it in GitHub Desktop.
celery tasks to upload files to 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
settings = {} | |
@celery.task | |
def upload_attachment_to_s3(msg_id, file_data): | |
conn = boto.connect_s3(settings['aws_access_key_id'], settings['aws_secret_access_key']) | |
#Connect to bucket and create key | |
bucket_url = settings['env'] + '.attachments' | |
b = conn.get_bucket(bucket_url) | |
key_url = str(msg_id) + file_data['filename'] | |
k = b.new_key(key_url) | |
k.set_contents_from_string(file_data['body']) | |
# Associate this url with the message | |
_db.add_attachment_to_message(ObjectId(msg_id), key_url) | |
@celery.task | |
def delete_attachment_from_s3(settings, msg_id, attach_ids): | |
conn = boto.connect_s3(settings['aws_access_key_id'], settings['aws_secret_access_key']) | |
#Connect to bucket and create key | |
bucket_url = settings['env'] + '.attachments' | |
b = conn.get_bucket(bucket_url) | |
for a in attach_ids: | |
k = b.new_key(a) | |
# Delete attachment from S3 and unlink from message | |
b.delete_key(k) | |
_db.remove_attachment_from_message(ObjectId(msg_id), a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment