Index: bin/lss3 |
diff --git a/bin/lss3 b/bin/lss3 |
index 1fba89d538445077a901c00d0571b755ec6d067b..377a5a597e76200dc4ee58fdb722dbdbf4ea0d9f 100755 |
--- a/bin/lss3 |
+++ b/bin/lss3 |
@@ -1,5 +1,6 @@ |
#!/usr/bin/env python |
import boto |
+from boto.s3.connection import OrdinaryCallingFormat |
def sizeof_fmt(num): |
for x in ['b ','KB','MB','GB','TB', 'XB']: |
@@ -8,21 +9,43 @@ def sizeof_fmt(num): |
num /= 1024.0 |
return "%3.1f %s" % (num, x) |
-def list_bucket(b): |
+def list_bucket(b, prefix=None): |
"""List everything in a bucket""" |
+ from boto.s3.prefix import Prefix |
+ from boto.s3.key import Key |
total = 0 |
- for k in b: |
+ query = b |
+ if prefix: |
+ if not prefix.endswith("/"): |
+ prefix = prefix + "/" |
+ query = b.list(prefix=prefix, delimiter="/") |
+ print "%s" % prefix |
+ num = 0 |
+ for k in query: |
+ num += 1 |
mode = "-rwx---" |
- for g in k.get_acl().acl.grants: |
- if g.id == None: |
- if g.permission == "READ": |
- mode = "-rwxr--" |
- elif g.permission == "FULL_CONTROL": |
- mode = "-rwxrwx" |
- print "%s\t%010s\t%s" % (mode, sizeof_fmt(k.size), k.name) |
- total += k.size |
- print "="*60 |
- print "TOTAL: \t%010s" % sizeof_fmt(total) |
+ if isinstance(k, Prefix): |
+ mode = "drwxr--" |
+ size = 0 |
+ else: |
+ size = k.size |
+ for g in k.get_acl().acl.grants: |
+ if g.id == None: |
+ if g.permission == "READ": |
+ mode = "-rwxr--" |
+ elif g.permission == "FULL_CONTROL": |
+ mode = "-rwxrwx" |
+ if isinstance(k, Key): |
+ print "%s\t%s\t%010s\t%s" % (mode, k.last_modified, |
+ sizeof_fmt(size), k.name) |
+ else: |
+ #If it's not a Key object, it doesn't have a last_modified time, so |
+ #print nothing instead |
+ print "%s\t%s\t%010s\t%s" % (mode, ' '*24, |
+ sizeof_fmt(size), k.name) |
+ total += size |
+ print "="*80 |
+ print "\t\tTOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num) |
def list_buckets(s3): |
"""List all the buckets""" |
@@ -31,9 +54,24 @@ def list_buckets(s3): |
if __name__ == "__main__": |
import sys |
- s3 = boto.connect_s3() |
+ |
+ pairs = [] |
+ mixedCase = False |
+ for name in sys.argv[1:]: |
+ if "/" in name: |
+ pairs.append(name.split("/",1)) |
+ else: |
+ pairs.append([name, None]) |
+ if pairs[-1][0].lower() != pairs[-1][0]: |
+ mixedCase = True |
+ |
+ if mixedCase: |
+ s3 = boto.connect_s3(calling_format=OrdinaryCallingFormat()) |
+ else: |
+ s3 = boto.connect_s3() |
+ |
if len(sys.argv) < 2: |
list_buckets(s3) |
else: |
- for name in sys.argv[1:]: |
- list_bucket(s3.get_bucket(name)) |
+ for name, prefix in pairs: |
+ list_bucket(s3.get_bucket(name), prefix) |