forked from SonyShrestha/VBP_Joint_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio_bucket.py
60 lines (52 loc) · 1.78 KB
/
minio_bucket.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
import docker
import time
from minio import Minio
from minio.error import S3Error
import configparser
import os
# Get the path to the parent parent directory
config_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir, os.pardir, os.pardir))
print(config_dir)
# Specify the path to config file
config_file_path = os.path.join(config_dir, "config.ini")
# reading the config file
config = configparser.ConfigParser()
config.read(config_file_path)
raw_data_folder_path= config['COMMON']['raw_data_dir']
endpoint = config['COMMON']['endpoint']
access_key = config['COMMON']['access_key']
secret_key = config['COMMON']['secret_key']
bucket_name = config['COMMON']['bucket_name']
def main():
# Create a client with the MinIO server playground, its access key
# and secret key.
client = Minio(endpoint,
access_key,
secret_key,
secure=False
)
# The file to upload, change this path if needed
# source_file = "D:/BDMA/UPC/BDM/P1/VBP_Joint_Project/data/raw/ApprovedFood.json"
# Make the bucket if it doesn't exist.
found = client.bucket_exists(bucket_name)
if not found:
client.make_bucket(bucket_name)
print("Created bucket", bucket_name)
else:
print("Bucket", bucket_name, "already exists")
# Upload the file, renaming it in the process
for filename in os.listdir(raw_data_folder_path):
filepath= os.path.join(raw_data_folder_path, filename)
destination_file= filename
client.fput_object(
bucket_name, destination_file, filepath,
)
print(
filepath, "successfully uploaded as object",
destination_file, "to bucket", bucket_name,
)
if __name__ == "__main__":
try:
main()
except S3Error as exc:
print("error occurred.", exc)