-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paths3.py
More file actions
63 lines (49 loc) · 1.38 KB
/
s3.py
File metadata and controls
63 lines (49 loc) · 1.38 KB
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
import json
from sys import argv, stdout
from functools import cache
import boto3
@cache
def get_s3():
return boto3.client('s3')
def parse_bkt_key(args: tuple[str, ...]) -> tuple[str, str]:
if len(args) == 1:
arg = args[0]
if arg.startswith('s3://'):
arg = arg[len('s3://'):]
bkt, key = arg.split('/', 1)
elif len(args) == 2:
bkt, key = args
else:
raise ValueError('Too many arguments')
return bkt, key
def get_etag(*args: str) -> str:
"""
Get the ETag of an S3 object.
Args:
bkt (str): The name of the S3 bucket
key (str): The key (path) of the object in the bucket
Returns:
str: The ETag value of the S3 object
"""
bkt, key = parse_bkt_key(args)
s3 = get_s3()
res = s3.head_object(Bucket=bkt, Key=key)
etag = res.get('ETag', '').strip('"')
return etag
def get_etags(*args: str) -> dict[str, str]:
bkt, key = parse_bkt_key(args)
s3 = get_s3()
res = s3.list_objects_v2(Bucket=bkt, Prefix=key)
etags = {
obj['Key']: obj['ETag'].strip('"')
for obj in res.get('Contents', [])
}
return etags
if __name__ == '__main__':
for arg in argv[1:]:
if arg.endswith('/'):
etags = get_etags(arg)
json.dump(etags, stdout, indent=2)
else:
etag = get_etag(arg)
print(etag)