| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 import boto | 2 import boto |
| 3 from boto.exception import S3ResponseError |
| 3 from boto.s3.connection import OrdinaryCallingFormat | 4 from boto.s3.connection import OrdinaryCallingFormat |
| 4 | 5 |
| 6 |
| 5 def sizeof_fmt(num): | 7 def sizeof_fmt(num): |
| 6 for x in ['b ','KB','MB','GB','TB', 'XB']: | 8 for x in ['b ', 'KB', 'MB', 'GB', 'TB', 'XB']: |
| 7 if num < 1024.0: | 9 if num < 1024.0: |
| 8 return "%3.1f %s" % (num, x) | 10 return "%3.1f %s" % (num, x) |
| 9 num /= 1024.0 | 11 num /= 1024.0 |
| 10 return "%3.1f %s" % (num, x) | 12 return "%3.1f %s" % (num, x) |
| 11 | 13 |
| 14 |
| 12 def list_bucket(b, prefix=None, marker=None): | 15 def list_bucket(b, prefix=None, marker=None): |
| 13 """List everything in a bucket""" | 16 """List everything in a bucket""" |
| 14 from boto.s3.prefix import Prefix | 17 from boto.s3.prefix import Prefix |
| 15 from boto.s3.key import Key | 18 from boto.s3.key import Key |
| 16 total = 0 | 19 total = 0 |
| 17 | 20 |
| 18 if prefix: | 21 if prefix: |
| 19 if not prefix.endswith("/"): | 22 if not prefix.endswith("/"): |
| 20 prefix = prefix + "/" | 23 prefix = prefix + "/" |
| 21 query = b.list(prefix=prefix, delimiter="/", marker=marker) | 24 query = b.list(prefix=prefix, delimiter="/", marker=marker) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 32 size = 0 | 35 size = 0 |
| 33 else: | 36 else: |
| 34 size = k.size | 37 size = k.size |
| 35 for g in k.get_acl().acl.grants: | 38 for g in k.get_acl().acl.grants: |
| 36 if g.id == None: | 39 if g.id == None: |
| 37 if g.permission == "READ": | 40 if g.permission == "READ": |
| 38 mode = "-rwxr--" | 41 mode = "-rwxr--" |
| 39 elif g.permission == "FULL_CONTROL": | 42 elif g.permission == "FULL_CONTROL": |
| 40 mode = "-rwxrwx" | 43 mode = "-rwxrwx" |
| 41 if isinstance(k, Key): | 44 if isinstance(k, Key): |
| 42 print "%s\t%s\t%010s\t%s" % (mode, k.last_modified, | 45 print "%s\t%s\t%010s\t%s" % (mode, k.last_modified, |
| 43 sizeof_fmt(size), k.name) | 46 sizeof_fmt(size), k.name) |
| 44 else: | 47 else: |
| 45 #If it's not a Key object, it doesn't have a last_modified time, so | 48 #If it's not a Key object, it doesn't have a last_modified time, so |
| 46 #print nothing instead | 49 #print nothing instead |
| 47 print "%s\t%s\t%010s\t%s" % (mode, ' '*24, | 50 print "%s\t%s\t%010s\t%s" % (mode, ' ' * 24, |
| 48 sizeof_fmt(size), k.name) | 51 sizeof_fmt(size), k.name) |
| 49 total += size | 52 total += size |
| 50 print "="*80 | 53 print "=" * 80 |
| 51 print "\t\tTOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num) | 54 print "\t\tTOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num) |
| 52 | 55 |
| 53 def list_buckets(s3): | 56 |
| 57 def list_buckets(s3, display_tags=False): |
| 54 """List all the buckets""" | 58 """List all the buckets""" |
| 55 for b in s3.get_all_buckets(): | 59 for b in s3.get_all_buckets(): |
| 56 print b.name | 60 print b.name |
| 61 if display_tags: |
| 62 try: |
| 63 tags = b.get_tags() |
| 64 for tag in tags[0]: |
| 65 print " %s:%s" % (tag.key, tag.value) |
| 66 except S3ResponseError as e: |
| 67 if e.status != 404: |
| 68 raise |
| 57 | 69 |
| 58 if __name__ == "__main__": | 70 |
| 71 def main(): |
| 59 import optparse | 72 import optparse |
| 60 import sys | 73 import sys |
| 61 | 74 |
| 62 if len(sys.argv) < 2: | 75 usage = "usage: %prog [options] [BUCKET1] [BUCKET2]" |
| 63 list_buckets(boto.connect_s3()) | 76 description = "List all S3 buckets OR list keys in the named buckets" |
| 77 parser = optparse.OptionParser(description=description, usage=usage) |
| 78 parser.add_option('-m', '--marker', |
| 79 help='The S3 key where the listing starts after it.') |
| 80 parser.add_option('-t', '--tags', action='store_true', |
| 81 help='Display tags when listing all buckets.') |
| 82 options, buckets = parser.parse_args() |
| 83 marker = options.marker |
| 84 |
| 85 if not buckets: |
| 86 list_buckets(boto.connect_s3(), options.tags) |
| 64 sys.exit(0) | 87 sys.exit(0) |
| 65 | 88 |
| 66 parser = optparse.OptionParser() | 89 if options.tags: |
| 67 parser.add_option('-m', '--marker', | 90 print "-t option only works for the overall bucket list" |
| 68 help='The S3 key where the listing starts after it.') | 91 sys.exit(1) |
| 69 options, buckets = parser.parse_args() | |
| 70 marker = options.marker | |
| 71 | 92 |
| 72 pairs = [] | 93 pairs = [] |
| 73 mixedCase = False | 94 mixedCase = False |
| 74 for name in buckets: | 95 for name in buckets: |
| 75 if "/" in name: | 96 if "/" in name: |
| 76 pairs.append(name.split("/",1)) | 97 pairs.append(name.split("/", 1)) |
| 77 else: | 98 else: |
| 78 pairs.append([name, None]) | 99 pairs.append([name, None]) |
| 79 if pairs[-1][0].lower() != pairs[-1][0]: | 100 if pairs[-1][0].lower() != pairs[-1][0]: |
| 80 mixedCase = True | 101 mixedCase = True |
| 81 | 102 |
| 82 if mixedCase: | 103 if mixedCase: |
| 83 s3 = boto.connect_s3(calling_format=OrdinaryCallingFormat()) | 104 s3 = boto.connect_s3(calling_format=OrdinaryCallingFormat()) |
| 84 else: | 105 else: |
| 85 s3 = boto.connect_s3() | 106 s3 = boto.connect_s3() |
| 86 | 107 |
| 87 for name, prefix in pairs: | 108 for name, prefix in pairs: |
| 88 list_bucket(s3.get_bucket(name), prefix, marker=marker) | 109 list_bucket(s3.get_bucket(name), prefix, marker=marker) |
| 110 |
| 111 |
| 112 if __name__ == "__main__": |
| 113 main() |
| OLD | NEW |