Repository navigation
Expand file tree
/
Copy paths3.py
More file actions
69 lines (57 loc) · 2.19 KB
/
Copy paths3.py
File metadata and controls
69 lines (57 loc) · 2.19 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
64
65
66
67
68
69
# Copyright (C) CVAT.ai Corporation
#
# SPDX-License-Identifier: MIT
from io import BytesIO
from typing import Optional
import boto3
from botocore.exceptions import ClientError
from shared.utils.config import MINIO_ENDPOINT_URL, MINIO_KEY, MINIO_SECRET_KEY
class S3Client:
def __init__(
self, endpoint_url: str, *, access_key: str, secret_key: str, bucket: Optional[str] = None
) -> None:
self.client = self._make_boto_client(
endpoint_url=endpoint_url, access_key=access_key, secret_key=secret_key
)
self.bucket = bucket
@staticmethod
def _make_boto_client(endpoint_url: str, *, access_key: str, secret_key: str):
s3 = boto3.resource(
"s3",
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
endpoint_url=endpoint_url,
)
return s3.meta.client
def create_file(self, filename: str, data: bytes = b"", *, bucket: Optional[str] = None):
bucket = bucket or self.bucket
assert bucket
self.client.put_object(Body=data, Bucket=bucket, Key=filename)
def remove_file(self, filename: str, *, bucket: Optional[str] = None):
bucket = bucket or self.bucket
assert bucket
self.client.delete_object(Bucket=bucket, Key=filename)
def file_exists(self, filename: str, *, bucket: Optional[str] = None) -> bool:
bucket = bucket or self.bucket
assert bucket
try:
self.client.head_object(Bucket=bucket, Key=filename)
return True
except ClientError as e:
if e.response["Error"]["Code"] == "404":
return False
else:
raise
def download_fileobj(self, key: str, *, bucket: Optional[str] = None) -> bytes:
bucket = bucket or self.bucket
assert bucket
with BytesIO() as data:
self.client.download_fileobj(Bucket=bucket, Key=key, Fileobj=data)
return data.getvalue()
def make_client(*, bucket: Optional[str] = None) -> S3Client:
return S3Client(
endpoint_url=MINIO_ENDPOINT_URL,
access_key=MINIO_KEY,
secret_key=MINIO_SECRET_KEY,
bucket=bucket,
)