pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 2 | """ |
| 3 | Upload a file to S3 |
| 4 | """ |
| 5 | |
| 6 | from sys import argv, stdout |
| 7 | from time import time |
| 8 | from os.path import basename |
| 9 | from optparse import OptionParser |
| 10 | |
| 11 | from boto.s3.key import Key |
| 12 | from boto.s3.connection import S3Connection |
| 13 | |
| 14 | |
Brian O'Connor | 0fed575 | 2015-05-04 18:15:29 -0700 | [diff] [blame] | 15 | def uploadFile( filename, dest=None, bucket=None, overwrite=False ): |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 16 | "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 / |
pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 23 | print('* Uploading', filename, 'to bucket', bucket, 'as', key) |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 24 | 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 |
pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 31 | print(( '\r%d bytes transmitted of %d (%.2f%%),' |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 32 | ' %.2f KB/sec ' % |
pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 33 | ( transmitted, size, percent, kbps ) ), end='\n') |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 34 | stdout.flush() |
| 35 | conn = S3Connection() |
| 36 | bucket = conn.get_bucket( bucket ) |
| 37 | k = Key( bucket ) |
| 38 | k.key = key |
Brian O'Connor | 0fed575 | 2015-05-04 18:15:29 -0700 | [diff] [blame] | 39 | if overwrite or not k.exists(): |
| 40 | k.set_contents_from_filename( filename, cb=callback, num_cb=100 ) |
pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 41 | print() |
Brian O'Connor | 0fed575 | 2015-05-04 18:15:29 -0700 | [diff] [blame] | 42 | elapsed = time() - start |
pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 43 | print("* elapsed time: %.2f seconds" % elapsed) |
Brian O'Connor | 0fed575 | 2015-05-04 18:15:29 -0700 | [diff] [blame] | 44 | else: |
pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 45 | print('file', basename( filename ), 'already exists in', bucket.name) |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 46 | |
Brian O'Connor | 8fccc73 | 2016-03-02 14:38:08 -0800 | [diff] [blame] | 47 | def 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 ) |
pierventre | 69db90c | 2022-05-06 23:06:20 +0200 | [diff] [blame] | 54 | print(bucket.get_acl()) |
Brian O'Connor | 8fccc73 | 2016-03-02 14:38:08 -0800 | [diff] [blame] | 55 | |
| 56 | |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 57 | if __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'Connor | 0fed575 | 2015-05-04 18:15:29 -0700 | [diff] [blame] | 68 | parser.add_option("-f", "--force", dest="overwrite", |
| 69 | help="Overwrite existing file") |
Brian O'Connor | 8fccc73 | 2016-03-02 14:38:08 -0800 | [diff] [blame] | 70 | parser.add_option("-v", "--verify", dest="verify", action="store_true", |
| 71 | help="Verify access to a bucket") |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 72 | (options, args) = parser.parse_args() |
Brian O'Connor | 8fccc73 | 2016-03-02 14:38:08 -0800 | [diff] [blame] | 73 | |
| 74 | if options.verify: |
| 75 | testAccess(options.bucket) |
| 76 | exit() |
| 77 | |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 78 | if len( args ) == 0: |
| 79 | parser.error("missing filenames") |
| 80 | for file in args: |
Brian O'Connor | 0fed575 | 2015-05-04 18:15:29 -0700 | [diff] [blame] | 81 | uploadFile( file, options.dest, options.bucket, options.overwrite ) |
Brian O'Connor | 6b30e1b | 2015-03-11 16:44:16 -0700 | [diff] [blame] | 82 | |
| 83 | #FIXME key and secret are unused |