Skip to content

Commit

Permalink
feat: modify bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
l-loic committed Apr 8, 2024
1 parent 18ddf68 commit 17ff747
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
9 changes: 4 additions & 5 deletions naas_python/domains/storage/StorageSchema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -211,4 +208,6 @@ class APIError(NaasException):
class StorageProviderNotFound(NaasException):
pass
class ServiceAuthenticationError(NaasException):
pass
class ServiceStatusError(NaasException):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from logging import getLogger
from datetime import datetime, timezone
from urllib.parse import urlparse
import mimetypes

logger = getLogger(__name__)

Expand All @@ -19,7 +20,8 @@
FileNotFoundError,
BadRequest,
ForbiddenError,
ServiceAuthenticationError
ServiceAuthenticationError,
ServiceStatusError
)

class S3StorageProviderAdaptor(IStorageProviderAdaptor):
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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))

Expand All @@ -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
Expand Down Expand Up @@ -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.")
Expand Down

0 comments on commit 17ff747

Please sign in to comment.