-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsobj
executable file
·155 lines (142 loc) · 5.43 KB
/
lsobj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
import sys
import re
import time
import logging
import boto.s3
from boto.exception import S3ResponseError
from qav.listpack import ListPack
from umobj.utils import umobj_logging, umobj_init, \
umobj_get_bucket_key_pair_from_string, print_word_list, sizeof_fmt, \
boto_error_handler
from umobj.options import umobj_parser, get_logging_level
from umobj.obj import Obj
from umobj.acl import all_users, get_formatted_grants_from_key
from umobj.key import get_metadata
def list_bucket(b, prefix=None, public=False, long_list=False, md5=False):
"""List everything in a bucket"""
from boto.s3.prefix import Prefix
from boto.s3.key import Key
total = 0
query = b
if prefix:
if not prefix.endswith("/"):
key = b.get_key(prefix)
if key:
query = [key]
else:
query = []
else:
query = b.list(prefix=prefix, delimiter="/")
num = 0
for k in query:
if not long_list:
print(k.name)
continue
num += 1
mode = "-rwx---"
if isinstance(k, Prefix):
mode = "drwxr--"
size = 0
else:
size = k.size
try:
for g in k.get_acl().acl.grants:
if g.id is None:
if g.permission == "READ":
mode = "-rwxr--"
elif g.permission == "FULL_CONTROL":
mode = "-rwxrwx"
except S3ResponseError:
mode = "-------"
if isinstance(k, Key):
try:
d = time.strptime(k.last_modified, "%Y-%m-%dT%H:%M:%S.%fZ")
except ValueError:
d = time.strptime(k.last_modified, "%a, %d %b %Y %H:%M:%S GMT")
print("%s\t%s\t%010s\t%s" %
(mode, time.strftime("%m/%d/%Y %H:%M:%S", d),
sizeof_fmt(size), k.name))
if md5:
etag = k.etag.strip('\"')
if re.search(r'-\d+', etag):
print(" MD5: Multipart")
else:
print(" MD5: %s" % k.etag.strip('\"'))
try:
grants = get_formatted_grants_from_key(k)
print(' ACLs: ' + print_word_list(grants))
metadata = get_metadata(b, k.name)
if metadata:
data = [(mdk, mdv) for mdk, mdv in metadata.iteritems()]
lp = ListPack(data, indentation=4)
print(' Metadata:')
print(lp)
except S3ResponseError:
pass
if public:
try:
for grant in k.get_acl().acl.grants:
if grant.permission == 'READ':
if grant.uri == all_users:
print(" %s" %
k.generate_url(expires_in=0,
query_auth=False))
except S3ResponseError:
pass
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
if long_list:
print("=" * 80)
if num == 1:
print("\t\tTOTAL: \t%010s \t%i File" % (sizeof_fmt(total), num))
else:
print("\t\tTOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num))
def list_buckets(s3, long_list=False):
"""List all the buckets"""
with boto_error_handler():
for b in s3.get_all_buckets():
print(b.name)
if long_list:
try:
grants = get_formatted_grants_from_key(b)
print(print_word_list(grants, preface=' ACLs:'))
except S3ResponseError:
pass
if __name__ == "__main__":
umobj_init()
description = 'List Object(s)'
parser = umobj_parser(description=description)
parser.add_long(help='Show long listing including ACLs')
parser.add_public(help='Show public URL for files if available')
parser.add_md5(help='Show MD5 sum if available')
parser.add_s3path(number='?', help='<BUCKET>[:KEY_PREFIX]')
args = parser.parse_args()
umobj_logging(get_logging_level(args)) # set up logging
logging.info("Running %s" % sys.argv)
if not Obj.connect(host=args.host,
port=args.port,
access_key=args.access_key,
secret_key=args.secret_key):
logging.error('Unable to contact object store.')
sys.exit(1)
if args.S3PATH is None:
list_buckets(Obj.conn, long_list=args.long_list)
else:
try:
bucket_name, key_prefix = \
umobj_get_bucket_key_pair_from_string(args.S3PATH)
bucket = Obj.conn.get_bucket(bucket_name)
except boto.exception.S3ResponseError:
print("ERROR: bucket %s does not exist." % bucket_name)
sys.exit(1)
if not key_prefix:
list_bucket(bucket, public=args.public,
long_list=args.long_list, md5=args.md5)
else:
list_bucket(bucket, key_prefix, public=args.public,
long_list=args.long_list, md5=args.md5)