-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathget_s3json.py
executable file
·87 lines (68 loc) · 2.78 KB
/
get_s3json.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
##############################################################################
# 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 [email protected]. #
##############################################################################
import asyncio
import sys
import os
import json
from aiobotocore.session import get_session
from aiohttp.client_exceptions import ClientOSError
if "CONFIG_DIR" not in os.environ:
os.environ["CONFIG_DIR"] = "../admin/config/"
from hsds.util.storUtil import getStorJSONObj, isStorObj, releaseStorageClient
from hsds.util.idUtil import getS3Key, isValidUuid
from hsds import config
# This is a utility to dump a JSON obj (group, dataset, ctype) given the
# the objects UUID
#
# Print usage and exit
#
def printUsage():
msg = "usage: python get_s3json [--bucket_name=<bucket>] "
msg += "[--aws_s3_gateway=<s3_endpoint>] objid "
print(msg)
print(" objid: s3 JSON obj to fetch")
msg = " Example: python get_s3json --aws_s3_gateway=http://192.168.99.100:9000"
msg += " --bucket_name=hsdsdev t-cf2fc310-996f-11e6-8ef6-0242ac110005"
print(msg)
sys.exit()
async def printS3Obj(app, obj_id):
try:
s3_key = getS3Key(obj_id)
obj_exists = await isStorObj(app, s3_key)
if not obj_exists:
print(f"key: {s3_key} not found")
return
json_obj = await getStorJSONObj(app, s3_key)
print(f"s3key {s3_key}:")
print(json.dumps(json_obj, sort_keys=True, indent=4))
except ValueError as ve:
print(f"Got ValueError exception: {ve}")
except ClientOSError as coe:
print(f"Got error: {coe}")
await releaseStorageClient(app)
def main():
if len(sys.argv) == 1 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
printUsage()
sys.exit(1)
obj_id = sys.argv[-1]
if not isValidUuid(obj_id):
print("Invalid obj id")
# we need to setup a asyncio loop to query s3
loop = asyncio.get_event_loop()
session = get_session()
app = {}
app["session"] = session
app["bucket_name"] = config.get("bucket_name")
app["loop"] = loop
loop.run_until_complete(printS3Obj(app, obj_id))
loop.close()
main()