Permalink
Browse files

Updated lss3 to allow for prefix based listing (more like actual ls)

Also updated s3.prefix.Prefix to inherit from object to allow us to do
object-y things.
  • Loading branch information...
1 parent 769819b commit cd3cadb1bbfb21f2a4698e4b28074110b44ebe20 @kopertop kopertop committed Mar 30, 2011
Showing with 48 additions and 31 deletions.
  1. +47 −30 bin/lss3
  2. +1 −1 boto/s3/prefix.py
View
@@ -2,38 +2,55 @@
import boto
def sizeof_fmt(num):
- for x in ['b ','KB','MB','GB','TB', 'XB']:
- if num < 1024.0:
- return "%3.1f %s" % (num, x)
- num /= 1024.0
- return "%3.1f %s" % (num, x)
+ for x in ['b ','KB','MB','GB','TB', 'XB']:
+ if num < 1024.0:
+ return "%3.1f %s" % (num, x)
+ num /= 1024.0
+ return "%3.1f %s" % (num, x)
-def list_bucket(b):
- """List everything in a bucket"""
- total = 0
- for k in b:
- 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)
+def list_bucket(b, prefix=None):
+ """List everything in a bucket"""
+ from boto.s3.prefix import Prefix
+ total = 0
+ 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---"
+ 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"
+ print "%s\t%010s\t%s" % (mode, sizeof_fmt(size), k.name)
+ total += size
+ print "="*60
+ print "TOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num)
def list_buckets(s3):
- """List all the buckets"""
- for b in s3.get_all_buckets():
- print b.name
+ """List all the buckets"""
+ for b in s3.get_all_buckets():
+ print b.name
if __name__ == "__main__":
- import sys
- 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))
+ import sys
+ s3 = boto.connect_s3()
+ if len(sys.argv) < 2:
+ list_buckets(s3)
+ else:
+ for name in sys.argv[1:]:
+ prefix = None
+ if "/" in name:
+ (name, prefix) = name.split("/",1)
+ list_bucket(s3.get_bucket(name), prefix)
View
@@ -19,7 +19,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-class Prefix:
+class Prefix(object):
def __init__(self, bucket=None, name=None):
self.bucket = bucket
self.name = name

0 comments on commit cd3cadb

Please sign in to comment.