#3 add pots and get for minio

This commit is contained in:
Martin Weick 2020-12-02 10:16:59 +01:00
parent f104795c17
commit 37b089881b
3 changed files with 47 additions and 2 deletions

View File

@ -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

View File

@ -220,3 +220,5 @@ DATA_UPLOAD_MAX_MEMORY_SIZE = 104857600
DROPBOX_OAUTH2_ACCESS_TOKEN = 'SDt1aqMQg7EAAAAAAAAAARV4CNnOSTjYLc05W2YAxIArG93DnaK9Si9VbwE-aBbQ' DROPBOX_OAUTH2_ACCESS_TOKEN = 'SDt1aqMQg7EAAAAAAAAAARV4CNnOSTjYLc05W2YAxIArG93DnaK9Si9VbwE-aBbQ'
DROPBOX_IMAGE_FOLDER = '/Apps/AIC Federated Storage Infrastructure/' DROPBOX_IMAGE_FOLDER = '/Apps/AIC Federated Storage Infrastructure/'
AWS_HOST= 'https://aic-fun-bucket-2020.s3.amazonaws.com/'

View File

@ -3,6 +3,7 @@ import json
import base64 import base64
from app_be.services.dropboxservice import DropboxService from app_be.services.dropboxservice import DropboxService
from app_be.services.minioservice import MinioService
from app_be.services.wrapperservice import WrapperService from app_be.services.wrapperservice import WrapperService
from app_be.views.mongo_db import MongoManager from app_be.views.mongo_db import MongoManager
@ -66,7 +67,7 @@ class ImageEndpoint:
del metadata['_id'] del metadata['_id']
dropbox_image_bytes = DropboxService.read_file(metadata['filename']) dropbox_image_bytes = DropboxService.read_file(metadata['filename'])
#minio_image_bytes = MinioService.read_file(metadata['filename'])
if dropbox_image_bytes is None: if dropbox_image_bytes is None:
return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404, safe=False) 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) decoded_image = WrapperService.unwrap_file(b64encoded_image)
if not DropboxService.create_file(filename, decoded_image): if not DropboxService.create_file(filename, decoded_image):
print("Could not save image to dropbox") 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}, return JsonResponse({'id': identifier, 'filename': filename},
safe=False) # 'metadata': metadata response beinhaltet ObjectId welche nicht serializable is? safe=False) # 'metadata': metadata response beinhaltet ObjectId welche nicht serializable is?