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 a46c500..7082b45 100644 --- a/middleware/app_be/views/rest_api.py +++ b/middleware/app_be/views/rest_api.py @@ -3,13 +3,12 @@ import logging import json from app_be.services.dropboxservice import DropboxService +from app_be.services.minioservice import MinioService from app_be.services.hashservice import create_sha512 from app_be.services.wrapperservice import WrapperService from app_be.views.mongo_db import MongoManager - from django.http import JsonResponse from django.http import HttpRequest - from rest_framework.decorators import api_view logger = logging.getLogger(__name__) @@ -66,6 +65,8 @@ 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) @@ -119,7 +120,8 @@ class ImageEndpoint: 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? diff --git a/minio_upload/mpv-shot0001.jpg b/minio_upload/mpv-shot0001.jpg new file mode 100644 index 0000000..4d76097 Binary files /dev/null and b/minio_upload/mpv-shot0001.jpg differ diff --git a/minio_upload/upload.py b/minio_upload/upload.py new file mode 100644 index 0000000..556f051 --- /dev/null +++ b/minio_upload/upload.py @@ -0,0 +1,31 @@ +import requests +import shutil + +host = 'https://aic-fun-bucket-2020.s3.amazonaws.com/' +remote_filename = 'testing2.jpg' + +url = host + remote_filename + +local_filename = 'mpv-shot0001.jpg' + +headers = {'Content-Type': 'image/jpeg'} + +# PUT image named 'mpv-shot0001.jpg' to 'testing2.jpg' +r = requests.put(url, data=open(local_filename, 'rb'), headers=headers) + +if r.status_code == 200: + print("Successfully uploaded a file!") +else: + print("Something went wrong") + exit() + +r = requests.get(url, stream=True) +if r.status_code == 200: + with open('file_downloaded.jpg', 'wb') as f: + r.raw.decode_content = True + shutil.copyfileobj(r.raw, f) + print("Successfully downloaded the file and saved it in 'file_downloaded.jpg'") +else: + print("Something went wrong while downloading the file!") + exit() + \ No newline at end of file