blob: c5dc8410846a9da341b9750a76beed06b9636c6c [file] [log] [blame]
pierventre69db90c2022-05-06 23:06:20 +02001#!/usr/bin/env python3
Brian O'Connor6b30e1b2015-03-11 16:44:16 -07002"""
3Upload a file to S3
4"""
5
6from sys import argv, stdout
7from time import time
8from os.path import basename
9from optparse import OptionParser
10
11from boto.s3.key import Key
12from boto.s3.connection import S3Connection
13
14
Brian O'Connor0fed5752015-05-04 18:15:29 -070015def uploadFile( filename, dest=None, bucket=None, overwrite=False ):
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070016 "Upload a file to a bucket"
17 if not bucket:
18 bucket = 'onos'
19 if not dest:
20 key = basename( filename )
21 else:
22 key = dest + basename( filename ) #FIXME add the /
pierventre69db90c2022-05-06 23:06:20 +020023 print('* Uploading', filename, 'to bucket', bucket, 'as', key)
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070024 stdout.flush()
25 start = time()
26 def callback( transmitted, size ):
27 "Progress callback for set_contents_from_filename"
28 elapsed = time() - start
29 percent = 100.0 * transmitted / size
30 kbps = .001 * transmitted / elapsed
pierventre69db90c2022-05-06 23:06:20 +020031 print(( '\r%d bytes transmitted of %d (%.2f%%),'
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070032 ' %.2f KB/sec ' %
pierventre69db90c2022-05-06 23:06:20 +020033 ( transmitted, size, percent, kbps ) ), end='\n')
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070034 stdout.flush()
35 conn = S3Connection()
36 bucket = conn.get_bucket( bucket )
37 k = Key( bucket )
38 k.key = key
Brian O'Connor0fed5752015-05-04 18:15:29 -070039 if overwrite or not k.exists():
40 k.set_contents_from_filename( filename, cb=callback, num_cb=100 )
pierventre69db90c2022-05-06 23:06:20 +020041 print()
Brian O'Connor0fed5752015-05-04 18:15:29 -070042 elapsed = time() - start
pierventre69db90c2022-05-06 23:06:20 +020043 print("* elapsed time: %.2f seconds" % elapsed)
Brian O'Connor0fed5752015-05-04 18:15:29 -070044 else:
pierventre69db90c2022-05-06 23:06:20 +020045 print('file', basename( filename ), 'already exists in', bucket.name)
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070046
Brian O'Connor8fccc732016-03-02 14:38:08 -080047def testAccess( bucket=None ):
48 "Verify access to a bucket"
49 if not bucket:
50 bucket = 'onos'
51
52 conn = S3Connection()
53 bucket = conn.get_bucket( bucket )
pierventre69db90c2022-05-06 23:06:20 +020054 print(bucket.get_acl())
Brian O'Connor8fccc732016-03-02 14:38:08 -080055
56
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070057if __name__ == '__main__':
58 usage = "Usage: %prog [options] <file to upload>"
59 parser = OptionParser(usage=usage)
60 parser.add_option("-b", "--bucket", dest="bucket",
61 help="Bucket on S3")
62 parser.add_option("-d", "--dest", dest="dest",
63 help="Destination path in bucket")
64 parser.add_option("-k", "--key", dest="awsKey",
65 help="Bucket on S3")
66 parser.add_option("-s", "--secret", dest="awsSecret",
67 help="Bucket on S3")
Brian O'Connor0fed5752015-05-04 18:15:29 -070068 parser.add_option("-f", "--force", dest="overwrite",
69 help="Overwrite existing file")
Brian O'Connor8fccc732016-03-02 14:38:08 -080070 parser.add_option("-v", "--verify", dest="verify", action="store_true",
71 help="Verify access to a bucket")
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070072 (options, args) = parser.parse_args()
Brian O'Connor8fccc732016-03-02 14:38:08 -080073
74 if options.verify:
75 testAccess(options.bucket)
76 exit()
77
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070078 if len( args ) == 0:
79 parser.error("missing filenames")
80 for file in args:
Brian O'Connor0fed5752015-05-04 18:15:29 -070081 uploadFile( file, options.dest, options.bucket, options.overwrite )
Brian O'Connor6b30e1b2015-03-11 16:44:16 -070082
83 #FIXME key and secret are unused