From f05571a81deb582cd1ad827ca18e5464b2975732 Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Fri, 8 Jan 2021 19:09:01 +0100 Subject: [PATCH] Add move/rename function for storages --- middleware/app_be/services/dropboxservice.py | 23 ++++++++++++++++++++ middleware/app_be/services/minioservice.py | 22 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/middleware/app_be/services/dropboxservice.py b/middleware/app_be/services/dropboxservice.py index bd85d98..b4f5f45 100644 --- a/middleware/app_be/services/dropboxservice.py +++ b/middleware/app_be/services/dropboxservice.py @@ -1,6 +1,7 @@ import requests from django.conf import settings import dropbox +from dropbox.exceptions import ApiError from app_be.services.StorageServiceInterface import StorageServiceInterface @@ -80,6 +81,28 @@ class DropboxService(StorageServiceInterface): print("DropboxService: Creating new version {}".format(identifier + '_' + str(i) + '.jpg')) return DropboxService.create_file(identifier + '_' + str(i) + '.jpg', file) + @staticmethod + def move_file(identifier_from: str, identifier_to: str) -> bool: + """Move/Rename a file on the dropbox storage. + + :param identifier_from: Location of the file to be moved + :param identifier_to: Location the file should be moved to + :return True on success, False otherwise + """ + print("DropboxService: Trying to move/rename file with identifier {} to identifier {}".format(identifier_from, identifier_to)) + + dbx = dropbox.Dropbox(settings.DROPBOX_OAUTH2_ACCESS_TOKEN) + url_from = settings.DROPBOX_IMAGE_FOLDER + identifier_from + '.jpg' + url_to = settings.DROPBOX_IMAGE_FOLDER + identifier_to + '.jpg' + + try: + dbx.files_move_v2(url_from, url_to) + except ApiError: + return False + + return True + + @staticmethod def delete_file(filename: str) -> bool: """Delete an file from the dropbox storage. diff --git a/middleware/app_be/services/minioservice.py b/middleware/app_be/services/minioservice.py index aa4b551..02e4046 100644 --- a/middleware/app_be/services/minioservice.py +++ b/middleware/app_be/services/minioservice.py @@ -73,6 +73,28 @@ class MinioService(StorageServiceInterface): return False return True + @staticmethod + def move_file(identifier_from: str, identifier_to: str) -> bool: + """Move/Rename a file on the S3 storage. + + Consists of a PUT method (copying the object) and a delete method. + :param identifier_from: Location of the file to be moved + :param identifier_to: Location the file should be moved to + :return True on success, False otherwise + """ + print("MinioService: Trying to move/rename file from {} to {}".format(identifier_from, identifier_to)) + + filename_from = identifier_from + '.jpg' + filename_to = identifier_to + '.jpg' + + url = settings.AWS_HOST + filename_to + headers = {'x-amz-copy-source': filename_from} + r = requests.put(url, headers=headers) + if r.status_code == 200: + print("Successfully moved/renamed the file {}!".format(filename_from)) + else: + print("Something went wrong while moving/renaming the file {}".format(filename_from)) + @staticmethod def read_file(filename: str) -> bytes: """Read file on the minio storage.