Hello. This is based on the Python Boto library. I created a class with a couple functions for storing and retrieving information (either publicly or privately) on Amazon S3. I added sufficient error handling to allow this code to be used in a production environment. Improvements and suggestions are always welcome. It also doubles as a command line utility for simply upload and and download files. I love that feature about Python. I wrote this so I could be DRY. I hope it helps you get your head in the cloud. Enjoy!
#!/usr/bin/env python # # simpleS3.py # # By: Alan Viars # Copyright Videntity Systems, Inc. 2009 # All rights Reseved. # License: New BSD # Last Updated: May 9, 2009 # # This was tested using Python 2.5 and Ubuntu Linux, but # it should run fine w/ other configurations. # You will need to install boto to get this library running # and of course you need an S3 account from Amazon. # See http://aws.amazon.com # # NOTES ON INSTALLING BOTO: # 1.7.a is latestversion of boto at the time of writing. # Execute the following from a command line prompt # > wget http://boto.googlecode.com/files/boto-1.7a.tar.gz # > tar zxvf boto-1.7a.tar.gz # > cd boto-1.7a # Run this as root or w/ admin privileges # > python setup.py install # > if on Ubuntu or Debian deravitive, use sudo like so: # > sudo python setup.py install """ simpleS3.py """ import sys from boto.s3.connection import S3Connection from boto.s3.key import Key import mimetypes #Set these to match your Amazon S3 Account AWS_ACCESS_KEY= '*****PUT_YOUR_KEY_HERE****' AWS_SECRET_ACCESS_KEY='*****PUT_YOUR_SECRET_KEY_HERE****' class SimpleS3: """ A very simple class library to simple store and retieve files in Amazon S3 Works with HTTPS/port 443 only (no HTTP/port 80) """ #Store a file in s3 def store_in_s3 (bucket, filename, local_filepath, public=False): """Store a file in s3""" url="" try: conn= S3Connection(AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY) b = conn.create_bucket(bucket) k=Key(b) k.key=filename mime = mimetypes.guess_type(filename) if mime==None: print "I couldn't guess MIME because" print "I couldn't detect a file ext." print "Using 'application/octet-stream'" print "as the default MIME instead." mime = "application/octet-stream" print "MIME Type = %s" % (mime) k.set_metadata("Content-Type", mime) k.set_contents_from_filename(local_filepath) if public==True: k.set_acl("public-read") url = "https://%s.s3.amazonaws.com/%s" % (bucket, k.key) except: print "Error in store_in_s3." print sys.exc_info() return url finally: return url #Get a file from s3 def get_from_s3 (bucket, filename, local_filepath ): """Get a file from s3""" retval = False try: conn= S3Connection(AWS_ACCESS_KEY, AWS_SECRET_ACCESS_KEY) b = conn.create_bucket(bucket) k = Key(b) k.key = filename k.get_contents_to_filename(local_filepath) retval = True except: print "Error in get_from_s3" print sys.exc_info() return retval finally: return retval # Our MAIN application which takes 3 command line arguments # Take in a mode, bucketname, filename, and public T/F. # if mode=PUT, then store the file in S3 # If mode=GET, then read the file from S3, # and write it to local disk if __name__ == "__main__": try: if sys.argv=='PUT': mode='PUT' elif sys.argv=='GET': mode='GET' else: mode='' if sys.argv in ('t', 'T','True','true','TRUE',): public=True else: public=False if mode=='PUT': print """Saving %s file to S3 bucket %s. Public = %s""" % (sys.argv,sys.argv,public ) url = store_in_s3 (sys.argv, sys.argv, sys.argv, public) if url != "": print "File saved in S3. URL is %s" % (url) else: print "Error in store_in_s3. No URL returned." elif mode=='GET': print "Reading %s from S3 and saving it to to %s" %(sys.argv, sys.argv) #get the file from S3 retval = get_from_s3 (sys.argv, sys.argv, sys.argv ) if retval==True: print "File %s saved locally." % (sys.argv) else: print "Error getting file from S3 and saving locally." else: print "Invalid mode. Pleas specify PUT or GET." except IndexError: print "Please supply all parameters in the form:" print "simples3.py <GET/PUT> <bucketname> <key> <local_filename> <Public? T/F>" print "Example:" print "simples3.py PUT mybucket my_key my_local_filename T" exit (1) except: print "Misc. Error. See below." print sys.exc_info() exit (1)
Download it here : SimpleS3.py