diff --git a/naas_python/domains/storage/StorageSchema.py b/naas_python/domains/storage/StorageSchema.py index 7258fbc..dd8a0f6 100644 --- a/naas_python/domains/storage/StorageSchema.py +++ b/naas_python/domains/storage/StorageSchema.py @@ -65,15 +65,13 @@ def post_workspace_storage_object(self, def get_workspace_storage_object(self, workspace_id: str, storage_name: Storage.__fields__['name'], - # storage_type: str, src_file: str, dst_file: str, ) -> bytes: raise NotImplementedError @abstractmethod - def create_workspace_storage_credentials(self, - # storage_type: str, + def create_workspace_storage_credentials(self, workspace_id: str, storage_name: Storage.__fields__['name'], ) -> None: @@ -84,7 +82,7 @@ class IStorageDomain(metaclass=ABCMeta): adaptor: IStorageAdaptor storage_provider_adaptors : List[IStorageProviderAdaptor] # storage_provider_adaptors : Map[str, IStorageProviderAdaptor] - # to be validated + #TODO to be validated @abstractmethod def create_workspace_storage(self, @@ -118,7 +116,6 @@ def list_workspace_storage_object(self, def delete_workspace_storage_object(self, workspace_id: str, storage_name: Storage.__fields__['name'], - # storage_prefix: Object.__fields__['prefix'], object_name: Object.__fields__['name'], ) -> None: raise NotImplementedError @@ -211,4 +208,6 @@ class APIError(NaasException): class StorageProviderNotFound(NaasException): pass class ServiceAuthenticationError(NaasException): + pass +class ServiceStatusError(NaasException): pass \ No newline at end of file diff --git a/naas_python/domains/storage/adaptors/secondary/providers/S3StorageProviderAdaptor.py b/naas_python/domains/storage/adaptors/secondary/providers/S3StorageProviderAdaptor.py index 5a152a6..b9be063 100644 --- a/naas_python/domains/storage/adaptors/secondary/providers/S3StorageProviderAdaptor.py +++ b/naas_python/domains/storage/adaptors/secondary/providers/S3StorageProviderAdaptor.py @@ -7,6 +7,7 @@ from logging import getLogger from datetime import datetime, timezone from urllib.parse import urlparse +import mimetypes logger = getLogger(__name__) @@ -19,7 +20,8 @@ FileNotFoundError, BadRequest, ForbiddenError, - ServiceAuthenticationError + ServiceAuthenticationError, + ServiceStatusError ) class S3StorageProviderAdaptor(IStorageProviderAdaptor): @@ -30,7 +32,7 @@ def __init__(self): super().__init__() self.MAX_RETRY_ATTEMPTS = 1 - self.naas_bucket = os.getenv("NAAS_ENDPOINT_URL") or "api-naas-storage" #TODO create naas-storage with versioning + self.naas_bucket = os.getenv("NAAS_ENDPOINT_URL") or "api-naas-storage" self.naas_credentials=os.path.expanduser("~/.naas/credentials") self.naas_workspace_id=None self.naas_workspace_id=None @@ -51,8 +53,13 @@ def post_workspace_storage_object(self, src_file: str, dst_file: str, ) -> None: + if dst_file.endswith('/'): dst_file = dst_file + os.path.basename(src_file) + + if dst_file == '.': + dst_file = os.path.basename(src_file) + key = f"{workspace_id}/{storage_name}/{dst_file}" key = self.__clean_path(key) @@ -61,11 +68,13 @@ def post_workspace_storage_object(self, reponse = self.__read_naas_credentials(workspace_id, storage_name) # print("token:", reponse) + content_type, _ = mimetypes.guess_type(src_file) + print("content_type:", content_type) s3 = boto3.client('s3') - response = s3.upload_file(Filename=src_file, Bucket=self.naas_bucket, Key=key) + response = s3.upload_file(Filename=src_file, Bucket=self.naas_bucket, Key=key, ExtraArgs={'ContentType': content_type}) # print("end uploading file:",response) return response - + except Exception as e: self.__handle_exceptions(str(e)) @@ -76,8 +85,12 @@ def get_workspace_storage_object(self, src_file: str, dst_file:str, ) -> bytes : - if dst_file.endswith('/') or dst_file.endswith('.'): + + if dst_file.endswith('/') : dst_file = dst_file + os.path.basename(src_file) + if dst_file == '.': + dst_file = os.path.basename(src_file) + filename=dst_file self.__clean_path(filename) object_key = workspace_id + "/" + storage_name + "/" + src_file @@ -242,8 +255,11 @@ def __handle_exceptions(self, exception: str) -> None: if isinstance(exception, ServiceAuthenticationError): raise ServiceAuthenticationError(exception) - - if "An error occurred (ExpiredToken)" in exception and "The provided token has expired." in exception: + elif isinstance(exception, ServiceStatusError): #TODO why not catch ? + raise ServiceStatusError(exception) + elif 'Workspace user' in str(exception) and 'not found' in str(exception): + raise ServiceAuthenticationError("Workspace user not found.") + elif "An error occurred (ExpiredToken)" in exception and "The provided token has expired." in exception: raise ExpiredToken("The provided token has expired. Please Retry.") elif "An error occurred (400)" in exception and "Bad Request" in exception : raise BadRequest(f"Bad request. Please retry in few seconds.")