Add update function for MinioService
This commit is contained in:
parent
bc82e3675d
commit
8371aba88b
@ -10,7 +10,6 @@ from app_be.services.StorageServiceInterface import StorageServiceInterface
|
||||
|
||||
|
||||
class MinioService(StorageServiceInterface):
|
||||
|
||||
name = "MinIO"
|
||||
|
||||
@staticmethod
|
||||
@ -19,7 +18,7 @@ class MinioService(StorageServiceInterface):
|
||||
try:
|
||||
url = settings.AWS_HOST
|
||||
r = requests.put(url)
|
||||
if not (r.status_code >= 200 and r.status_code < 500):
|
||||
if not (200 <= r.status_code < 500):
|
||||
return False
|
||||
except:
|
||||
return False
|
||||
@ -45,6 +44,30 @@ class MinioService(StorageServiceInterface):
|
||||
return False
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def update_file(filename: str, file: bytes) -> bool:
|
||||
"""Update a file on the minio storage.
|
||||
|
||||
Finds the latest version of the current file and adds another
|
||||
version to the storage.
|
||||
:param filename: The filename without the version.
|
||||
:param file: Byte representation of the file to save as new version
|
||||
:return True if successful, False otherwise
|
||||
"""
|
||||
try:
|
||||
headers = {'Content-Type': 'binary/octet-stream'}
|
||||
i = 0
|
||||
while MinioService.read_file(filename + '_' + str(i)) is not False:
|
||||
i = i + 1
|
||||
url = settings.AWS_HOST + filename + '_' + str(i)
|
||||
r = requests.put(url, data=file, headers=headers)
|
||||
if r.status_code == 200:
|
||||
print("Successfully uploaded a new version with identifier %s!".format(filename + '_' + str(i)))
|
||||
else:
|
||||
print("Something went wrong while updating")
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def read_file(filename: str) -> bytes:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user