valar 1.0.18__py3-none-any.whl → 1.0.19__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of valar might be problematic. Click here for more details.

@@ -0,0 +1,91 @@
1
+ import json
2
+ from io import BytesIO
3
+
4
+ from django.conf import settings
5
+ from minio import Minio
6
+ from urllib3 import HTTPResponse
7
+
8
+ def minio_upload_object(bucket_name, object_name, _bytes):
9
+ client = __get_minio_client__()
10
+ __create_bucket__(bucket_name, client)
11
+ file_data = BytesIO(_bytes)
12
+ file_size = len(_bytes) # file.siz
13
+ client.put_object(bucket_name=bucket_name, object_name=object_name, data=file_data, length=file_size)
14
+ return f'{bucket_name}/{object_name}'
15
+
16
+ def minio_remove_object(bucket_name, object_name):
17
+ client = __get_minio_client__()
18
+ client.remove_object(bucket_name=bucket_name, object_name=object_name)
19
+
20
+ def minio_remove_path(path):
21
+ [bucket_name, object_name] = path.split('/')
22
+ client = __get_minio_client__()
23
+ client.remove_object(bucket_name=bucket_name, object_name=object_name)
24
+
25
+
26
+ def minio_read_object(bucket_name, object_name) -> HTTPResponse:
27
+ client = __get_minio_client__()
28
+ return client.get_object(bucket_name=bucket_name, object_name=object_name)
29
+
30
+
31
+
32
+
33
+
34
+ def __get_minio_client__(bucket_name=None):
35
+ options = settings.MINIO_SETTINGS
36
+ client = Minio(**options)
37
+ if bucket_name:
38
+ __create_bucket__(bucket_name, client)
39
+ return client
40
+
41
+ def __create_bucket__(bucket_name, client=None):
42
+ client = client or __get_minio_client__()
43
+ exists = client.bucket_exists(bucket_name)
44
+ if not exists:
45
+ client.make_bucket(bucket_name)
46
+ policy = generate_policy(bucket_name)
47
+ client.set_bucket_policy(bucket_name, policy)
48
+
49
+
50
+ def get_minio_bucket_name(entity):
51
+ value = f'{settings.BASE_DIR.name}.{entity}'
52
+ bucket_name = value.replace('_','-').lower()
53
+ __create_bucket__(bucket_name)
54
+ return bucket_name
55
+
56
+ def get_minio_object_name(_id, prop, file_name):
57
+ return f"{_id}-{prop}-{file_name}"
58
+
59
+ def generate_policy(bucket_name):
60
+ return json.dumps({
61
+ "Version": "2012-10-17",
62
+ "Statement": [
63
+ {
64
+ "Sid": "",
65
+ "Effect": "Allow",
66
+ "Principal": {"AWS": "*"},
67
+ "Action": "s3:GetBucketLocation",
68
+ "Resource": f"arn:aws:s3:::{bucket_name}"
69
+ },
70
+ {
71
+ "Sid": "",
72
+ "Effect": "Allow",
73
+ "Principal": {"AWS": "*"},
74
+ "Action": "s3:ListBucket",
75
+ "Resource": f"arn:aws:s3:::{bucket_name}"
76
+ },
77
+ {
78
+ "Sid": "",
79
+ "Effect": "Allow",
80
+ "Principal": {"AWS": "*"},
81
+ "Action": "s3:GetObject",
82
+ "Resource": f"arn:aws:s3:::{bucket_name}/*"
83
+ },
84
+ {
85
+ "Sid": "",
86
+ "Effect": "Allow",
87
+ "Principal": {"AWS": "*"},
88
+ "Action": "s3:PutObject",
89
+ "Resource": f"arn:aws:s3:::{bucket_name}/*"
90
+ }
91
+ ]})