Add move/rename function for storages

This commit is contained in:
Tobias Eidelpes 2021-01-08 19:09:01 +01:00
parent 27b4f50f69
commit f05571a81d
2 changed files with 45 additions and 0 deletions

View File

@ -1,6 +1,7 @@
import requests import requests
from django.conf import settings from django.conf import settings
import dropbox import dropbox
from dropbox.exceptions import ApiError
from app_be.services.StorageServiceInterface import StorageServiceInterface from app_be.services.StorageServiceInterface import StorageServiceInterface
@ -80,6 +81,28 @@ class DropboxService(StorageServiceInterface):
print("DropboxService: Creating new version {}".format(identifier + '_' + str(i) + '.jpg')) print("DropboxService: Creating new version {}".format(identifier + '_' + str(i) + '.jpg'))
return DropboxService.create_file(identifier + '_' + str(i) + '.jpg', file) 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 @staticmethod
def delete_file(filename: str) -> bool: def delete_file(filename: str) -> bool:
"""Delete an file from the dropbox storage. """Delete an file from the dropbox storage.

View File

@ -73,6 +73,28 @@ class MinioService(StorageServiceInterface):
return False return False
return True 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 @staticmethod
def read_file(filename: str) -> bytes: def read_file(filename: str) -> bytes:
"""Read file on the minio storage. """Read file on the minio storage.