Created
June 25, 2014 14:52
-
-
Save johnpaulhayes/01f0cc826f3875c93b33 to your computer and use it in GitHub Desktop.
Upload a file to S3 bucket
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
""" | |
Requirements: | |
AWS Account | |
Valid Access key and secret | |
A valid bucket setup | |
""" | |
from boto.s3.connection import S3Connection | |
from boto.s3.key import Key | |
import sys | |
config = { | |
"AWS_ACCESS_KEY": 'XXXXXXX', | |
"AWS_SECRET": 'XXXXXXX' | |
} | |
class AWS(object): | |
def __init__(self, bucket_name, access_key, secret): | |
self.client_name = 'api_client' | |
self.access_key = access_key | |
self.secret = secret | |
self.s3_connection = S3Connection(self.access_key, self.secret) | |
self.bucket = self.s3_connection.get_bucket(bucket_name) | |
self.bucket_key = Key(self.bucket) | |
def add_object_to_bucket(self, file_location, key_name): | |
self.bucket_key.key = key_name | |
try: | |
self.bucket_key.set_contents_from_filename(file_location, encrypt_key=True) | |
self.bucket_key.set_acl('public-read') | |
return "http://a-bucket.s3.amazonaws.com/"+key_name | |
except Exception, e: | |
return False, e.message | |
cmd_args = sys.argv | |
print "Backing up file %s to %s" % (cmd_args[1], "S3 bucket bubil-mongo-backups") | |
aws = AWS('a-bucket', config["AWS_ACCESS_KEY"], config["AWS_SECRET"]) | |
backup_result = aws.add_object_to_bucket(file_location=cmd_args[1], key_name=cmd_args[1]) | |
print "Backup result: ", backup_result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment