#!/bin/env python """ S3Uploader.py David Janes BlogMatrix 2006.08.05 Upload files to Amazon S3 """ __id__ = "$Id: EditAccount.py,v 1.1.2.8 2006/03/29 22:51:54 gt_private Exp $" __version__ = "$Revision: 1.1.2.8 $" import sys import os import os.path import time import re import pprint import types import mimetypes import S3 try: from bm_log import Log except ImportError: def Log(*as, **av): pprint.pprint((as, av)) class Uploader: def __init__(self, bucket, access_key, secret_key, extensions = None, debug = False, root = None): self.bucket = bucket self.access_key = access_key self.secret_key = secret_key self.debug = debug self.extensions = extensions self.ignore_dot = True self.root = root assert(self.bucket), "--bucket is required" assert(self.access_key), "--access-key is required" assert(self.secret_key), "--secret-key is required" def Connect(self): self.connection = S3.AWSAuthConnection(self.access_key, self.secret_key) acl = self.connection.get_bucket_acl(self.bucket) if acl.http_response.status != 200: Log("Could not create a session", status = acl.http_response.status, body = acl.body) assert(False) def UploadDirectory(self, directory): if self.root: directory = os.path.join(self.root, directory) local_root = self.root else: local_root = directory local_root = local_root.rstrip("/") + "/" link_directories = [] Log(directory = directory) for walk_root, dirs, files in os.walk(directory, topdown = True): if self.ignore_dot: dirs[:] = filter(lambda s: not s.startswith("."), dirs) for file in files: if self.ignore_dot and file.startswith("."): continue if self.extensions == None: is_match = True else: is_match = False for ext in self.extensions: ext = "." + ext.lstrip(".") if file.endswith(ext): is_match = True break if not is_match: continue file_path = os.path.join(walk_root, file) if not os.path.isfile(file_path): continue file_name = file_path[len(local_root):] self.UploadFile(file_path, file_name) def UploadFile(self, file_path, file_name): Log("uploading file", file_path = file_path, file_name = file_name) if self.debug: return try: fin = open(file_path, 'rb') file_data = fin.read() fin.close() except OSError: Log("could not read file", file_path = file_path) return content_type = mimetypes.guess_type(file_path)[0] if not content_type: content_type = 'text/plain' self.connection.put( self.bucket, file_name, S3.S3Object(file_data), { 'x-amz-acl': 'public-read', 'Content-Type': content_type, }, ) if __name__ == '__main__': from optparse import OptionParser parser = OptionParser() parser.add_option( "", "--debug", default = False, action = "store_true", dest = "debug", help = "") parser.add_option( "", "--bucket", dest = "bucket", help = "Amazon S3 Bucket") parser.add_option( "", "--access-key", dest = "access_key", help = "Amazon S3 Access Key (required)") parser.add_option( "", "--secret-key", dest = "secret_key", help = "Amazon S3 Secret Access Key (command line prompt if missing)") parser.add_option( "", "--root", dest = "root", help = "All directories are made relative to this (optional) root") parser.add_option( "", "--directory", action = "append", dest = "directories", help = "Upload files from this directory (default: .)") parser.add_option( "", "--extension", action = "append", dest = "extensions", help = "Upload files matching this extension (default: all files)") (options, args) = parser.parse_args() try: if options.extensions: new_extensions = [] for ext1 in options.extensions: for ext2 in ext1.split(","): new_extensions.append("." + ext2.lstrip(".")) options.extensions = new_extensions if not options.secret_key: print "Secret Key:", try: os.system("stty -echo") options.secret_key = raw_input() finally: try: os.system("stty echo") except: pass uploader = Uploader( bucket = options.bucket, access_key = options.access_key, secret_key = options.secret_key, debug = options.debug, extensions = options.extensions, root = options.root, ) uploader.Connect() if not options.directories: options.directories = [ '.' ] for directory in options.directories: uploader.UploadDirectory(directory) except: Log(exception = True) parser.print_help(sys.stderr) sys.exit(1)