Merge branch '3-minio_api' into 'master'
Resolve "MinIO_API" Closes #3 See merge request aic20/g4t2!5
This commit is contained in:
commit
03a4036b08
41
middleware/app_be/services/minioservice.py
Normal file
41
middleware/app_be/services/minioservice.py
Normal 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
|
||||||
@ -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/'
|
||||||
|
|||||||
@ -3,13 +3,12 @@ import logging
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
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.hashservice import create_sha512
|
from app_be.services.hashservice import create_sha512
|
||||||
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
|
||||||
|
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
|
|
||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@ -66,6 +65,8 @@ 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,
|
return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404,
|
||||||
safe=False)
|
safe=False)
|
||||||
@ -119,7 +120,8 @@ class ImageEndpoint:
|
|||||||
|
|
||||||
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?
|
||||||
|
|
||||||
|
|||||||
BIN
minio_upload/mpv-shot0001.jpg
Normal file
BIN
minio_upload/mpv-shot0001.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 360 KiB |
31
minio_upload/upload.py
Normal file
31
minio_upload/upload.py
Normal file
@ -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()
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user