diff --git a/middleware/app_be/services/minioservice.py b/middleware/app_be/services/minioservice.py new file mode 100644 index 0000000..992adb7 --- /dev/null +++ b/middleware/app_be/services/minioservice.py @@ -0,0 +1,41 @@ +import requests +from django.conf import settings + +class MinioService: + + @staticmethod + def create_file(filename: str, file: bytes) -> bool: + """Create / save (or overwrite) an file on the minio storage. + + :param filename: filename + :param file: bytes representation of the file + :return: Whether file was successfully uploaded + """ + try: + url= settings.AWS_HOST + filename + headers = {'Content-Type': 'binary/octet-stream'} + r = requests.put(url,data=file,headers=headers) + if r.status_code == 200: + print("Successfully uploaded a file!") + else: + print("Something went wrong") + except: + return False + return True + + + @staticmethod + def read_file(filename: str) -> bytes: + """Read file on the minio storage. + + :param filename: filename + :return: bytes representation of the file or None on error + """ + file_bytes: bytes + try: + url = settings.AWS_HOST + filename + response = requests.get(url, stream=True) + file_bytes=response.content + except: + print("Error downloading file from minio") + return file_bytes \ No newline at end of file diff --git a/middleware/app_be/settings.py b/middleware/app_be/settings.py index a95c2d2..47a9414 100644 --- a/middleware/app_be/settings.py +++ b/middleware/app_be/settings.py @@ -220,3 +220,5 @@ DATA_UPLOAD_MAX_MEMORY_SIZE = 104857600 DROPBOX_OAUTH2_ACCESS_TOKEN = 'SDt1aqMQg7EAAAAAAAAAARV4CNnOSTjYLc05W2YAxIArG93DnaK9Si9VbwE-aBbQ' DROPBOX_IMAGE_FOLDER = '/Apps/AIC Federated Storage Infrastructure/' + +AWS_HOST= 'https://aic-fun-bucket-2020.s3.amazonaws.com/' diff --git a/middleware/app_be/views/rest_api.py b/middleware/app_be/views/rest_api.py index 54fc50b..7e51af7 100644 --- a/middleware/app_be/views/rest_api.py +++ b/middleware/app_be/views/rest_api.py @@ -3,6 +3,7 @@ import json import base64 from app_be.services.dropboxservice import DropboxService +from app_be.services.minioservice import MinioService from app_be.services.wrapperservice import WrapperService from app_be.views.mongo_db import MongoManager @@ -66,7 +67,7 @@ class ImageEndpoint: del metadata['_id'] dropbox_image_bytes = DropboxService.read_file(metadata['filename']) - + #minio_image_bytes = MinioService.read_file(metadata['filename']) if dropbox_image_bytes is None: return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404, safe=False) @@ -101,6 +102,7 @@ class ImageEndpoint: decoded_image = WrapperService.unwrap_file(b64encoded_image) if not DropboxService.create_file(filename, decoded_image): print("Could not save image to dropbox") - + if not MinioService.create_file(filename, decoded_image): + print("Could not save image to minio") return JsonResponse({'id': identifier, 'filename': filename}, safe=False) # 'metadata': metadata response beinhaltet ObjectId welche nicht serializable is?