-
Notifications
You must be signed in to change notification settings - Fork 4
/
aws_client.py
executable file
·171 lines (136 loc) · 5.94 KB
/
aws_client.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import logging
from pprint import pprint
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import tostring
import requests
import urllib3
from aws_requests_auth.aws_auth import AWSRequestsAuth
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class AwsClient:
def __init__(self, aws_s3, access_key, secret_key, host, region, service) -> None:
super().__init__()
self.aws_s3 = aws_s3
self.auth = AWSRequestsAuth(aws_access_key=access_key,
aws_secret_access_key=secret_key,
aws_host=host,
aws_region=region,
aws_service=service)
def get_buckets(self, params=None):
rsp = self._ose_get(self._buckets_url(), params=params)
assert rsp.status_code == 200
return rsp
@staticmethod
def _dump_rsp(rsp):
log.debug('===== RESPONSE DUMP =====')
log.debug('status code {}'.format(rsp.status_code))
for h in rsp.headers:
log.debug('{}={}'.format(h, rsp.headers[h]))
log.debug(rsp.text)
log.debug('\n')
def create_bucket(self, bkt):
rsp = self._ose_put(self._bucket_url(bkt))
assert rsp.status_code == 200
return rsp
def head_bucket(self, bkt):
rsp = self._ose_head(self._bucket_url(bkt))
return rsp
def delete_bucket(self, bkt):
rsp = self._ose_delete(self._bucket_url(bkt))
assert rsp.status_code == 204
return rsp
def get_bucket_versioning(self, bkt):
rsp = self._ose_get(self._bucket_url(bkt), params={'versioning': ''})
assert rsp.status_code == 200
return rsp
def enable_bucket_versioning(self, bkt):
rsp = self._ose_put(self._bucket_url(bkt), params={'versioning': ''},
data=self._enable_bucket_versioning_body())
assert rsp.status_code == 200
return rsp
def suspend_bucket_versioning(self, bkt):
rsp = self._ose_put(self._bucket_url(bkt), params={'versioning': ''},
data=self._suspend_bucket_versioning_body())
assert rsp.status_code == 200
return rsp
def create_object(self, bkt, *objects, data=None):
rsp = self._ose_put(self._object_url(bkt, *objects), data=data)
assert rsp.status_code == 200
return rsp
def get_object(self, bkt, *objects, params=None):
rsp = self._ose_get(self._object_url(bkt, *objects), params=params)
return rsp
def head_object(self, bkt, *objects, params=None):
rsp = self._ose_head(self._object_url(bkt, *objects), params=params)
return rsp
def delete_object(self, bkt, *objects, params=None):
rsp = self._ose_delete(self._object_url(bkt, *objects), params=params)
assert rsp.status_code == 204
return rsp
def delete_version(self, bkt, obj, version):
rsp = self._ose_delete(self._object_url(bkt, obj), params={'versionId': version})
assert rsp.status_code == 204
return rsp
def delete_all_versions(self, bkt, obj):
rsp = self.list_versions(bkt)
assert rsp.status_code == 200
for version in rsp.json()['versions']:
rsp = self.delete_version(bkt, obj, version['versionId'])
assert rsp.status_code == 204
def list_objects(self, bkt, params=None):
rsp = self._ose_get(self._bucket_url(bkt), params=params)
assert rsp.status_code == 200
return rsp
def list_versions(self, bucket, params=None):
rsp = self._ose_get(self._versions_url(bucket), params=params)
assert rsp.status_code == 200
return rsp
def _buckets_url(self):
return self.aws_s3
def _bucket_url(self, bkt):
return '{}/{}'.format(self.aws_s3, bkt)
def _object_url(self, bkt, *objects):
url = '{}/{}/'.format(self.aws_s3, bkt)
for obj in objects:
url += obj
return url
def _versions_url(self, bkt):
return '{}/{}?versions'.format(self.aws_s3, bkt)
def _ose_get(self, url, params=None):
pprint('REQUEST GET =====> {} {}'.format(url, params))
rsp = requests.get(url, params=params, auth=self.auth, verify=False)
self._dump_rsp(rsp)
return rsp
def _ose_head(self, url, params=None):
pprint('REQUEST HEAD =====> {} {}'.format(url, params))
rsp = requests.head(url, params=params, auth=self.auth, verify=False)
self._dump_rsp(rsp)
return rsp
def _ose_put(self, url, params=None, data=None):
log.debug('REQUEST PUT =====> {} {}\n'.format(url, params))
rsp = requests.put(url, params=params, data=data, auth=self.auth, verify=False)
self._dump_rsp(rsp)
return rsp
def _ose_post(self, url, params=None, data=None):
pprint('REQUEST POST =====> {} {}'.format(url, params))
rsp = requests.post(url, params=params, data=data, auth=self.auth, verify=False)
self._dump_rsp(rsp)
return rsp
def _ose_delete(self, url, params=None):
pprint('REQUEST DELETE =====> {} {}'.format(url, params))
rsp = requests.delete(url, params=params, auth=self.auth, verify=False)
self._dump_rsp(rsp)
return rsp
def _enable_bucket_versioning_body(self):
return self._dict_to_xml('VersioningConfiguration', {'Status': 'Enabled'})
def _suspend_bucket_versioning_body(self):
return self._dict_to_xml('VersioningConfiguration', {'Status': 'Suspended'})
@staticmethod
def _dict_to_xml(tag, d):
elem = Element(tag)
for key, val in d.items():
child = Element(key)
child.text = str(val)
elem.append(child)
return tostring(elem)