Created
April 23, 2015 11:12
-
-
Save amalgjose/5fb2fb7d92fd9a0d722c to your computer and use it in GitHub Desktop.
Python program for sending a file 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
__author__ = 'Amal G Jose' | |
import boto | |
import ntpath | |
from boto.s3.connection import S3Connection | |
class SimpleDataUploader(object): | |
##Initializer | |
def __init__(self): | |
try: | |
self.s3_bucket = "mybucket" | |
self.s3_path = "data" | |
self.aws_access_key = "XXXXXXXXXXXX" | |
self.aws_secret_key = "XXXXXXXXXXXX" | |
self.s3_conn = boto.connect_s3(aws_access_key_id=self.aws_access_key, | |
aws_secret_access_key=self.aws_secret_key, | |
calling_format=boto.s3.connection.OrdinaryCallingFormat()) | |
except Exception, e: | |
print "Exception occurred in the initializer : " + str(e) | |
##This method will upload files to S3 | |
def upload_to_s3(self, local_file_name): | |
try: | |
path, file_name = ntpath.split(local_file_name) | |
bucket = self.s3_conn.get_bucket(self.s3_bucket) | |
key = bucket.new_key(file_name) | |
key.set_contents_from_filename(local_file_name) | |
print "File uploaded successfully" | |
except Exception, e: | |
print "Exception while uploading file to S3 : " + str(e) | |
if __name__ == '__main__': | |
uploader = SimpleDataUploader() | |
uploader.upload_to_s3("data.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment