blob: 8b02d3b2cd3a9e237c6a8830ade425125e20bd9d [file] [log] [blame]
Brian O'Connor6b30e1b2015-03-11 16:44:16 -07001#!/usr/bin/python
2
3"""
4Upload a file to S3
5"""
6
7from sys import argv, stdout
8from time import time
9from os.path import basename
10from optparse import OptionParser
11
12from boto.s3.key import Key
13from boto.s3.connection import S3Connection
14
15
16def uploadFile( bucket, filename, dest=None ):
17 "Upload a file to a bucket"
18 if not bucket:
19 bucket = 'onos'
20 if not dest:
21 key = basename( filename )
22 else:
23 key = dest + basename( filename ) #FIXME add the /
24 print '* Uploading', filename, 'to bucket', bucket, 'as', key
25 stdout.flush()
26 start = time()
27 def callback( transmitted, size ):
28 "Progress callback for set_contents_from_filename"
29 elapsed = time() - start
30 percent = 100.0 * transmitted / size
31 kbps = .001 * transmitted / elapsed
32 print ( '\r%d bytes transmitted of %d (%.2f%%),'
33 ' %.2f KB/sec ' %
34 ( transmitted, size, percent, kbps ) ),
35 stdout.flush()
36 conn = S3Connection()
37 bucket = conn.get_bucket( bucket )
38 k = Key( bucket )
39 k.key = key
40 k.set_contents_from_filename( filename, cb=callback, num_cb=100 )
41 print
42 elapsed = time() - start
43 print "* elapsed time: %.2f seconds" % elapsed
44
45if __name__ == '__main__':
46 usage = "Usage: %prog [options] <file to upload>"
47 parser = OptionParser(usage=usage)
48 parser.add_option("-b", "--bucket", dest="bucket",
49 help="Bucket on S3")
50 parser.add_option("-d", "--dest", dest="dest",
51 help="Destination path in bucket")
52 parser.add_option("-k", "--key", dest="awsKey",
53 help="Bucket on S3")
54 parser.add_option("-s", "--secret", dest="awsSecret",
55 help="Bucket on S3")
56 (options, args) = parser.parse_args()
57
58 if len( args ) == 0:
59 parser.error("missing filenames")
60 for file in args:
61 uploadFile( options.bucket, file, options.dest )
62
63 #FIXME key and secret are unused