diff options
Diffstat (limited to 'scripts/mias3.py')
| -rw-r--r-- | scripts/mias3.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/scripts/mias3.py b/scripts/mias3.py new file mode 100644 index 0000000..9a276d6 --- /dev/null +++ b/scripts/mias3.py @@ -0,0 +1,76 @@ +import datetime +import mimetypes +import os +import sys +import time +import S3 + +CONN = None +AWS_ACCESS_KEY_ID = 'AKIAJAQK4CDDP6I6SNVA' +AWS_SECRET_ACCESS_KEY = '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' +BUCKET_NAME = 'dumpfm' +COUNTER = 0 + +def retry_func(f, count): + try: + f() + except: + if count <= 1: raise + else: + print 'Error! retrying %s more time(s)' % (count - 1) + retry_func(f, count - 1) + +def upload_file(path): + global COUNTER + path = os.path.normpath(path) + if path == '.' or not os.path.isfile(path): + return + filedata = open(path, 'rb').read() + size = os.path.getsize(path) + content_type = mimetypes.guess_type(path)[0] + if not content_type: + content_type = 'text/plain' + + path = path.replace('\\', '/') # Windows hack + start = time.time() + def do_upload(): + CONN.put(BUCKET_NAME, path, S3.S3Object(filedata), + {'x-amz-acl': 'public-read', 'Content-Type': 'video/x-flv'}) + retry_func(do_upload, 3) + + ms_took = (time.time() - start) * 1000 + print "uploaded %s (%0.0fms) (%sKB)" % (path, ms_took, size / 1024) + COUNTER += 1 + +def upload_directory(path): + for f in sorted(os.listdir(path)): + subpath = os.path.join(path, f) + if os.path.isdir(subpath): + upload_directory(subpath) + else: + upload_file(subpath) + +def do_upload(path): + global CONN + CONN = S3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) + + start = time.time() + + if os.path.isdir(path): + upload_directory(path) + else: + upload_file(path) + + s_took = (time.time() - start) + print "uploaded %s files in %0.0fs" % (COUNTER, s_took) + + +if __name__ == "__main__": + if len(sys.argv) == 1: + print 'usage: s3upload.py path' + sys.exit(1) + + args = sys.argv[1:] + for path in args: + do_upload(path) + print |
