-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathminio.py
53 lines (44 loc) · 1.32 KB
/
minio.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
# coding=utf-8
"""
Utilities for s3.
.. note:: Returning files from s3 bucket.
"""
import os
import s3fs
class S3FileSyetem:
"""Return s3 client."""
def __init__(self, client, bucket):
"""Return a MinioClient object."""
self.s3 = s3fs.S3FileSystem(
key=os.environ.get(f'{client}_S3_ACCESS_KEY', ''),
secret=os.environ.get(f'{client}_S3_SECRET_KEY', ''),
endpoint_url=os.environ.get(f'{client}_S3_ENDPOINT_URL', ''),
client_kwargs={
'region_name': os.environ.get(
f'{client}_S3_REGION_NAME', ''
)
}
)
self.bucket = bucket
def open_file(self, filepath):
"""Open and read file."""
return self.s3.open(
f's3://{self.bucket}/{filepath}',
'r',
encoding="utf8",
errors='ignore'
)
def files(self, folder=None):
"""Lists files in an S3 bucket."""
if folder:
files = self.s3.ls(f'{self.bucket}/{folder}')
else:
files = self.s3.ls(f'{self.bucket}')
files.sort()
return files
def exist(self, path):
"""Check if path is exist."""
if path:
return self.s3.exists(f'{self.bucket}/{path}')
else:
return True