/
serializer.py
74 lines (64 loc) · 2.43 KB
/
serializer.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
from dict2xml import dict2xml
from typing import List, Dict, Optional
from datetime import datetime
def bucket_object(obj: Dict) -> Dict:
return {
"ETag": "eTag",
"Key": obj.get("key"),
"LastModified": str(datetime.now()),
"Owner": {"DisplayName": obj.get("display_name"), "ID": obj.get("id")},
"Size": obj.get("size"),
"StorageClass": "standard",
}
def bucket_metadata(bucket: Dict) -> Dict:
return {
"Bucket": {
"Name": bucket.get("name"),
"CreationDate": bucket.get("creation_date"),
},
}
def bucket_owner(owner: Dict) -> Dict:
return {
"DisplayName": owner.get("display_name"),
"ID": owner.get("id"),
}
# https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListBuckets.html
# listBuckets
def list_buckets(buckets: List, owner: Dict, config: Optional[Dict] = {}) -> str:
bucket_data = [bucket_metadata(bucket) for bucket in buckets]
list_buckets_response = {
"ListAllMyBucketsResult": {
"Buckets": bucket_data,
"Owner": bucket_owner(owner),
}
}
print("ListBucketResponse", list_buckets_response)
return dict2xml(list_buckets_response)
# https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html
# ListObjectsV2 Response
def list_bucket_objects(bucket: str, objects: List, config: Optional[Dict] = {}) -> str:
list_bucket_v2_response = {
"ListBucketResult": {
"@": {"xmlns": "http://doc.s3.amazonaws.com/2006-03-01/"},
"IsTruncated": False,
"KeyCount": len(objects),
"MaxKeys": len(objects),
"Name": bucket,
"Prefix": config.get("prefix") or "",
"Delimiter": config.get("delimiter") or "",
"MaxKeys": config.get("max-keys") or 1000,
# TODO: Support delimiter query parameter
# "CommonPrefixes": [
# {"Prefix": "CommonPrefix"},
# ],
"EncodingType": "UTF-8",
"KeyCount": len(objects),
"ContinuationToken": config.get("continuation-token") or "",
"NextContinuationToken": "nextContinuationToken",
"StartAfter": config.get("start-after") or "",
}
}
if len(objects) > 0:
contents = [bucket_object(o) for o in objects]
list_bucket_v2_response["ListBucketResult"]["Contents"] = contents
return dict2xml(list_bucket_v2_response)