@@ -452,7 +452,6 @@ async def create_file_upload_links(
452452 # NOTE: if this gets called successively with the same file_id, and
453453 # there was a multipart upload in progress beforehand, it MUST be
454454 # cancelled to prevent unwanted costs in AWS
455- _logger .debug ("Cleaning pending uploads for file_id=%s" , file_id )
456455 await self ._clean_pending_upload (
457456 TypeAdapter (SimcoreS3FileID ).validate_python (file_id )
458457 )
@@ -462,7 +461,6 @@ async def create_file_upload_links(
462461 ): # NOTE: Delete is not needed for directories that are synced via an external tool (rclone/aws s3 cli).
463462 # ensure file is deleted first in case it already exists
464463 # https://github.com/ITISFoundation/osparc-simcore/pull/5108
465- _logger .debug ("Attempting to delete file_id=%s" , file_id )
466464 await self .delete_file (
467465 user_id = user_id ,
468466 file_id = file_id ,
@@ -491,19 +489,23 @@ async def create_file_upload_links(
491489 file_size_bytes
492490 ):
493491 # create multipart links
494- _logger .debug ("Creating multipart upload links for file_id=%s" , file_id )
495- assert file_size_bytes # nosec
496- multipart_presigned_links = await get_s3_client (
497- self .app
498- ).create_multipart_upload_links (
499- bucket = fmd .bucket_name ,
500- object_key = fmd .file_id ,
501- file_size = file_size_bytes ,
502- expiration_secs = get_application_settings (
492+ with log_context (
493+ logger = _logger ,
494+ level = logging .DEBUG ,
495+ msg = f"Creating multipart upload links for { file_id = } " ,
496+ ):
497+ assert file_size_bytes # nosec
498+ multipart_presigned_links = await get_s3_client (
503499 self .app
504- ).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS ,
505- sha256_checksum = fmd .sha256_checksum ,
506- )
500+ ).create_multipart_upload_links (
501+ bucket = fmd .bucket_name ,
502+ object_key = fmd .file_id ,
503+ file_size = file_size_bytes ,
504+ expiration_secs = get_application_settings (
505+ self .app
506+ ).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS ,
507+ sha256_checksum = fmd .sha256_checksum ,
508+ )
507509 # update the database so we keep the upload id
508510 fmd .upload_id = multipart_presigned_links .upload_id
509511 await FileMetaDataRepository .instance (get_db_engine (self .app )).upsert (
@@ -515,29 +517,35 @@ async def create_file_upload_links(
515517 )
516518 if link_type == LinkType .PRESIGNED :
517519 # create single presigned link
518- _logger .debug (
519- "Creating single presigned upload link for file_id=%s" , file_id
520- )
521- single_presigned_link = await get_s3_client (
522- self .app
523- ).create_single_presigned_upload_link (
524- bucket = self .simcore_bucket_name ,
525- object_key = fmd .file_id ,
526- expiration_secs = get_application_settings (
520+ with log_context (
521+ logger = _logger ,
522+ level = logging .DEBUG ,
523+ msg = f"Creating single presigned upload link for { file_id = } " ,
524+ ):
525+ single_presigned_link = await get_s3_client (
527526 self .app
528- ).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS ,
529- )
527+ ).create_single_presigned_upload_link (
528+ bucket = self .simcore_bucket_name ,
529+ object_key = fmd .file_id ,
530+ expiration_secs = get_application_settings (
531+ self .app
532+ ).STORAGE_DEFAULT_PRESIGNED_LINK_EXPIRATION_SECONDS ,
533+ )
530534 return UploadLinks (
531535 [single_presigned_link ],
532536 file_size_bytes or MAX_LINK_CHUNK_BYTE_SIZE [link_type ],
533537 )
534538
535539 # user wants just the s3 link
536- _logger .debug ("Creating S3 link for file_id=%s" , file_id )
537- s3_link = get_s3_client (self .app ).compute_s3_url (
538- bucket = self .simcore_bucket_name ,
539- object_key = TypeAdapter (SimcoreS3FileID ).validate_python (file_id ),
540- )
540+ with log_context (
541+ logger = _logger ,
542+ level = logging .DEBUG ,
543+ msg = f"Compute S3 link for file_id={ file_id } " ,
544+ ):
545+ s3_link = get_s3_client (self .app ).compute_s3_url (
546+ bucket = self .simcore_bucket_name ,
547+ object_key = TypeAdapter (SimcoreS3FileID ).validate_python (file_id ),
548+ )
541549 return UploadLinks (
542550 [s3_link ], file_size_bytes or MAX_LINK_CHUNK_BYTE_SIZE [link_type ]
543551 )
@@ -694,46 +702,52 @@ async def delete_file(
694702 # Only use this in those circumstances where a collaborator requires to delete a file (the current
695703 # permissions model will not allow him to do so, even though this is a legitimate action)
696704 # SEE https://github.com/ITISFoundation/osparc-simcore/issues/5159
705+ with log_context (
706+ logger = _logger , level = logging .DEBUG , msg = f"Deleting file { file_id = } "
707+ ):
708+ if enforce_access_rights :
709+ can = await AccessLayerRepository .instance (
710+ get_db_engine (self .app )
711+ ).get_file_access_rights (user_id = user_id , file_id = file_id )
712+ if not can .delete :
713+ raise FileAccessRightError (access_right = "delete" , file_id = file_id )
697714
698- if enforce_access_rights :
699- can = await AccessLayerRepository .instance (
700- get_db_engine (self .app )
701- ).get_file_access_rights (user_id = user_id , file_id = file_id )
702- if not can .delete :
703- raise FileAccessRightError (access_right = "delete" , file_id = file_id )
704-
705- try :
706- await get_s3_client (self .app ).delete_objects_recursively (
707- bucket = self .simcore_bucket_name ,
708- prefix = file_id ,
709- )
710- except S3KeyNotFoundError :
711- _logger .warning ("File %s not found in S3" , file_id )
712- # we still need to clean up the database entry (it exists)
713- # and to invalidate the size of the parent directory
715+ try :
716+ await get_s3_client (self .app ).delete_objects_recursively (
717+ bucket = self .simcore_bucket_name ,
718+ prefix = file_id ,
719+ )
720+ except S3KeyNotFoundError :
721+ _logger .warning ("File %s not found in S3" , file_id )
722+ # we still need to clean up the database entry (it exists)
723+ # and to invalidate the size of the parent directory
714724
715- async with transaction_context (get_db_engine (self .app )) as connection :
716- file_meta_data_repo = FileMetaDataRepository .instance (
717- get_db_engine (self .app )
718- )
719- await file_meta_data_repo .delete (connection = connection , file_ids = [file_id ])
720-
721- if parent_dir_fmds := await file_meta_data_repo .list_filter_with_partial_file_id (
722- connection = connection ,
723- user_or_project_filter = UserOrProjectFilter (
724- user_id = user_id , project_ids = []
725- ),
726- file_id_prefix = compute_file_id_prefix (file_id , 2 ),
727- partial_file_id = None ,
728- is_directory = True ,
729- sha256_checksum = None ,
730- ):
731- parent_dir_fmd = max (parent_dir_fmds , key = lambda fmd : len (fmd .file_id ))
732- parent_dir_fmd .file_size = UNDEFINED_SIZE
733- await file_meta_data_repo .upsert (
734- connection = connection , fmd = parent_dir_fmd
725+ async with transaction_context (get_db_engine (self .app )) as connection :
726+ file_meta_data_repo = FileMetaDataRepository .instance (
727+ get_db_engine (self .app )
728+ )
729+ await file_meta_data_repo .delete (
730+ connection = connection , file_ids = [file_id ]
735731 )
736732
733+ if parent_dir_fmds := await file_meta_data_repo .list_filter_with_partial_file_id (
734+ connection = connection ,
735+ user_or_project_filter = UserOrProjectFilter (
736+ user_id = user_id , project_ids = []
737+ ),
738+ file_id_prefix = compute_file_id_prefix (file_id , 2 ),
739+ partial_file_id = None ,
740+ is_directory = True ,
741+ sha256_checksum = None ,
742+ ):
743+ parent_dir_fmd = max (
744+ parent_dir_fmds , key = lambda fmd : len (fmd .file_id )
745+ )
746+ parent_dir_fmd .file_size = UNDEFINED_SIZE
747+ await file_meta_data_repo .upsert (
748+ connection = connection , fmd = parent_dir_fmd
749+ )
750+
737751 async def delete_project_simcore_s3 (
738752 self , user_id : UserID , project_id : ProjectID , node_id : NodeID | None = None
739753 ) -> None :
@@ -963,17 +977,22 @@ async def create_soft_link(
963977 return convert_db_to_model (await file_meta_data_repo .insert (fmd = target ))
964978
965979 async def _clean_pending_upload (self , file_id : SimcoreS3FileID ) -> None :
966- with suppress (FileMetaDataNotFoundError ):
967- fmd = await FileMetaDataRepository .instance (get_db_engine (self .app )).get (
968- file_id = file_id
969- )
970- if is_valid_managed_multipart_upload (fmd .upload_id ):
971- assert fmd .upload_id # nosec
972- await get_s3_client (self .app ).abort_multipart_upload (
973- bucket = self .simcore_bucket_name ,
974- object_key = file_id ,
975- upload_id = fmd .upload_id ,
976- )
980+ with log_context (
981+ logger = _logger ,
982+ level = logging .DEBUG ,
983+ msg = f"Cleaning pending uploads for { file_id = } " ,
984+ ):
985+ with suppress (FileMetaDataNotFoundError ):
986+ fmd = await FileMetaDataRepository .instance (
987+ get_db_engine (self .app )
988+ ).get (file_id = file_id )
989+ if is_valid_managed_multipart_upload (fmd .upload_id ):
990+ assert fmd .upload_id # nosec
991+ await get_s3_client (self .app ).abort_multipart_upload (
992+ bucket = self .simcore_bucket_name ,
993+ object_key = file_id ,
994+ upload_id = fmd .upload_id ,
995+ )
977996
978997 async def _clean_expired_uploads (self ) -> None :
979998 """this method will check for all incomplete updates by checking
0 commit comments