Created
October 3, 2013 06:27
-
-
Save payoung/6805856 to your computer and use it in GitHub Desktop.
This is a python class wrapped around a number of common boto methods that I use to access AWS. Makes the code a little cleaner and abstracts away the details of making the connection to aws. At the moment it only contains methods to push and pull data to S3. WIll most likely add a method to retrieve a list files in a S3 bucket, and possibly add…
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
import boto | |
import boto.s3.connection | |
# credentials.py should contain the access_key and secret_key which are needed for | |
# the boto S3 connection | |
import credentials as cr | |
class AwsConnect(object): | |
def __init__(self, bucket): | |
self.bucket = bucket | |
self.connect() | |
# Connect to S3 | |
def connect(self): | |
conn = boto.connect_s3( | |
aws_access_key_id = cr.access_key, | |
aws_secret_access_key = cr.secret_key, | |
host = 's3-us-west-2.amazonaws.com', | |
calling_format = boto.s3.connection.OrdinaryCallingFormat(), | |
) | |
# Connect to bucket | |
self.cbucket = conn.get_bucket(self.bucket) | |
def pull(self, filepathin, filepathout): | |
key = self.cbucket.get_key(filepathin) | |
key.get_contents_to_filename(filepathout) | |
def push(self, filepathin, filepathout): | |
key = self.cbucket.new_key(filepathin) | |
key.set_contents_from_filename(filepathout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment