-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathlist_objects.py
executable file
·131 lines (108 loc) · 3.67 KB
/
list_objects.py
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
##############################################################################
# Copyright by The HDF Group. #
# All rights reserved. #
# #
# This file is part of HSDS (HDF5 Scalable Data Service), Libraries and #
# Utilities. The full HSDS copyright notice, including #
# terms governing use, modification, and redistribution, is contained in #
# the file COPYING, which can be found at the root of the source code #
# distribution tree. If you do not have access to this file, you may #
# request a copy from help@hdfgroup.org. #
##############################################################################
import asyncio
import sys
import os
if "CONFIG_DIR" not in os.environ:
os.environ["CONFIG_DIR"] = "../admin/config/"
from aiobotocore.session import get_session
from hsds.util.storUtil import getStorKeys, releaseStorageClient
from hsds import config
# This is a utility to list all objects in the bucket
#
# Print usage and exit
#
def printUsage():
usage = " python list_objects.py [--prefix <prefix> ] "
usage += "[--deliminator deliminator] [--showstats]"
print(usage)
sys.exit()
def getS3KeysCallback(app, s3keys):
print("getS3KeysCallback, {} items".format(len(s3keys)))
if isinstance(s3keys, list):
for s3key in s3keys:
print(s3key)
else:
for s3key in s3keys.keys():
etag = None
obj_size = None
lastModified = None
item = s3keys[s3key]
print("item:", item)
if "ETag" in item:
etag = item["ETag"]
if "Size" in item:
obj_size = item["Size"]
if "LastModified" in item:
lastModified = item["LastModified"]
print("{}: {} {} {}".format(s3key, etag, obj_size, lastModified))
async def listObjects(app, prefix="", deliminator="", suffix="", showstats=False):
await getStorKeys(
app,
prefix=prefix,
deliminator=deliminator,
suffix=suffix,
include_stats=showstats,
callback=getS3KeysCallback,
)
await releaseStorageClient(app)
def main():
if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
printUsage()
prefix = ""
deliminator = ""
suffix = ""
showstats = False
argn = 1
while argn < len(sys.argv):
arg = sys.argv[argn]
val = None
if len(sys.argv) > argn + 1:
val = sys.argv[argn + 1]
if arg == "--prefix":
prefix = val
argn += 2
elif arg == "--deliminator":
deliminator = val
argn += 2
elif arg == "--suffix":
suffix = val
argn += 2
elif arg == "--showstats":
showstats = True
argn += 1
else:
printUsage()
print("prefix:", prefix)
print("deliminator:", deliminator)
print("suffix:", suffix)
print("showstats:", showstats)
# we need to setup a asyncio loop to query s3
loop = asyncio.get_event_loop()
app = {}
app["bucket_name"] = config.get("bucket_name")
app["loop"] = loop
session = get_session()
app["session"] = session
app["filter_map"] = {}
loop.run_until_complete(
listObjects(
app,
prefix=prefix,
deliminator=deliminator,
suffix=suffix,
showstats=showstats,
)
)
loop.close()
print("done!")
main()