OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 import boto | 2 import boto |
| 3 from boto.s3.connection import OrdinaryCallingFormat |
3 | 4 |
4 def sizeof_fmt(num): | 5 def sizeof_fmt(num): |
5 for x in ['b ','KB','MB','GB','TB', 'XB']: | 6 for x in ['b ','KB','MB','GB','TB', 'XB']: |
6 if num < 1024.0: | 7 if num < 1024.0: |
7 return "%3.1f %s" % (num, x) | 8 return "%3.1f %s" % (num, x) |
8 num /= 1024.0 | 9 num /= 1024.0 |
9 return "%3.1f %s" % (num, x) | 10 return "%3.1f %s" % (num, x) |
10 | 11 |
11 def list_bucket(b): | 12 def list_bucket(b, prefix=None): |
12 """List everything in a bucket""" | 13 """List everything in a bucket""" |
| 14 from boto.s3.prefix import Prefix |
| 15 from boto.s3.key import Key |
13 total = 0 | 16 total = 0 |
14 for k in b: | 17 query = b |
| 18 if prefix: |
| 19 if not prefix.endswith("/"): |
| 20 prefix = prefix + "/" |
| 21 query = b.list(prefix=prefix, delimiter="/") |
| 22 print "%s" % prefix |
| 23 num = 0 |
| 24 for k in query: |
| 25 num += 1 |
15 mode = "-rwx---" | 26 mode = "-rwx---" |
16 for g in k.get_acl().acl.grants: | 27 if isinstance(k, Prefix): |
17 if g.id == None: | 28 mode = "drwxr--" |
18 if g.permission == "READ": | 29 size = 0 |
19 mode = "-rwxr--" | 30 else: |
20 elif g.permission == "FULL_CONTROL": | 31 size = k.size |
21 mode = "-rwxrwx" | 32 for g in k.get_acl().acl.grants: |
22 print "%s\t%010s\t%s" % (mode, sizeof_fmt(k.size), k.name) | 33 if g.id == None: |
23 total += k.size | 34 if g.permission == "READ": |
24 print "="*60 | 35 mode = "-rwxr--" |
25 print "TOTAL: \t%010s" % sizeof_fmt(total) | 36 elif g.permission == "FULL_CONTROL": |
| 37 mode = "-rwxrwx" |
| 38 if isinstance(k, Key): |
| 39 print "%s\t%s\t%010s\t%s" % (mode, k.last_modified, |
| 40 sizeof_fmt(size), k.name) |
| 41 else: |
| 42 #If it's not a Key object, it doesn't have a last_modified time, so |
| 43 #print nothing instead |
| 44 print "%s\t%s\t%010s\t%s" % (mode, ' '*24, |
| 45 sizeof_fmt(size), k.name) |
| 46 total += size |
| 47 print "="*80 |
| 48 print "\t\tTOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num) |
26 | 49 |
27 def list_buckets(s3): | 50 def list_buckets(s3): |
28 """List all the buckets""" | 51 """List all the buckets""" |
29 for b in s3.get_all_buckets(): | 52 for b in s3.get_all_buckets(): |
30 print b.name | 53 print b.name |
31 | 54 |
32 if __name__ == "__main__": | 55 if __name__ == "__main__": |
33 import sys | 56 import sys |
34 s3 = boto.connect_s3() | 57 |
| 58 pairs = [] |
| 59 mixedCase = False |
| 60 for name in sys.argv[1:]: |
| 61 if "/" in name: |
| 62 pairs.append(name.split("/",1)) |
| 63 else: |
| 64 pairs.append([name, None]) |
| 65 if pairs[-1][0].lower() != pairs[-1][0]: |
| 66 mixedCase = True |
| 67 |
| 68 if mixedCase: |
| 69 s3 = boto.connect_s3(calling_format=OrdinaryCallingFormat()) |
| 70 else: |
| 71 s3 = boto.connect_s3() |
| 72 |
35 if len(sys.argv) < 2: | 73 if len(sys.argv) < 2: |
36 list_buckets(s3) | 74 list_buckets(s3) |
37 else: | 75 else: |
38 for name in sys.argv[1:]: | 76 for name, prefix in pairs: |
39 list_bucket(s3.get_bucket(name)) | 77 list_bucket(s3.get_bucket(name), prefix) |
OLD | NEW |