Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

lss3: code cleanup and addition of displaying bucket tags #2132

Merged
merged 3 commits into from
Mar 5, 2014
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 41 additions & 19 deletions bin/lss3
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import boto
from boto.s3.connection import OrdinaryCallingFormat


def sizeof_fmt(num):
for x in ['b ','KB','MB','GB','TB', 'XB']:
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, prefix=None, marker=None):
"""List everything in a bucket"""
from boto.s3.prefix import Prefix
Expand Down Expand Up @@ -39,45 +41,61 @@ def list_bucket(b, prefix=None, marker=None):
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)
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)
print "%s\t%s\t%010s\t%s" % (mode, ' ' * 24,
sizeof_fmt(size), k.name)
total += size
print "="*80
print "=" * 80
print "\t\tTOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num)

def list_buckets(s3):

def list_buckets(s3, display_tags=False):
"""List all the buckets"""
for b in s3.get_all_buckets():
print b.name
if display_tags:
try:
tags = b.get_tags()
for tag in tags[0]:
print " %s:%s" % (tag.key, tag.value)
except:
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we let the user know that there was an issue fetching tags instead of failing silently and making it seem like there are no tags? This could be dumped into stderr instead of stdout and just contain a quick message about the problem and which bucket was involved.


if __name__ == "__main__":
def main():
import optparse
import sys

if len(sys.argv) < 2:
list_buckets(boto.connect_s3())
sys.exit(0)

parser = optparse.OptionParser()
usage = "usage: %prog [options] [BUCKET1] [BUCKET2]"
description = "List all S3 buckets OR list keys in the named buckets"
parser = optparse.OptionParser(description=description, usage=usage)
parser.add_option('-m', '--marker',
help='The S3 key where the listing starts after it.')
parser.add_option('-t', '--tags', action='store_true',
help='Display tags when listing all buckets.')
options, buckets = parser.parse_args()
marker = options.marker

if not buckets:
list_buckets(boto.connect_s3(), options.tags)
sys.exit(0)

if options.tags:
print "-t option only works for the overall bucket list"
sys.exit(1)

pairs = []
mixedCase = False
for name in buckets:
if "/" in name:
pairs.append(name.split("/",1))
else:
pairs.append([name, None])
if pairs[-1][0].lower() != pairs[-1][0]:
mixedCase = True
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())
Expand All @@ -86,3 +104,7 @@ if __name__ == "__main__":

for name, prefix in pairs:
list_bucket(s3.get_bucket(name), prefix, marker=marker)


if __name__ == "__main__":
main()