-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.py
46 lines (38 loc) · 1.38 KB
/
s3.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
import requests
from minio import Minio
from minio.credentials import WebIdentityProvider
from config import Config
def jwt():
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
from_multipart_payload = {
"client_id": "s3",
"client_secret": Config.S3_SECRET,
"grant_type": "client_credentials"
}
res = requests.post(
Config.S3_TOKENURI, data=from_multipart_payload, headers=headers
)
print("Got res" + str(res))
return res.json()
class Connection:
def __init__(self):
self.client = None
def connect(self):
provider = WebIdentityProvider(jwt_provider_func=lambda: jwt(),
sts_endpoint=Config.S3_STS_ENDPOINT,
duration_seconds=86400
)
self.client = Minio(Config.S3_HOST, credentials=provider)
def process_all(self, bucket_name):
import importer
for obj in self.client.list_objects(bucket_name, recursive=True):
print(".", end='')
if not obj.is_dir:
try:
response = self.client.get_object(bucket_name, obj.object_name)
importer.from_s3_into_db(response.data.decode())
finally:
response.close()
response.release_conn()