# -*- coding: utf-8 -*-"""Upload file from local to s3."""importtypingasTfrompathlib_mateimportPathfromfunc_argsimportNOTHING,resolve_kwargsfrom.resolve_s3_clientimportresolve_s3_clientfrom..better_client.uploadimportupload_dirfrom..typeimportPathTypefrom..awsimportcontextifT.TYPE_CHECKING:# pragma: no coverfrom.s3pathimportS3Pathfromboto3.s3.transferimportTransferConfigfromboto_session_managerimportBotoSesManagerfrommypy_boto3_s3importS3Clientfrommypy_boto3_s3.type_defsimportUploadFile
[docs]classUploadAPIMixin:""" A mixin class that implements upload method. """
[docs]defupload_file(self:"S3Path",path:PathType,overwrite:bool=False,extra_args:dict=NOTHING,callback:callable=NOTHING,config:"TransferConfig"=NOTHING,bsm:T.Optional[T.Union["BotoSesManager","S3Client"]]=None,):""" Upload a file from local file system to targeted S3 path Example:: >>> s3path = S3Path("bucket", "artifacts", "deployment.zip") >>> s3path.upload_file(path="/tmp/build/deployment.zip", overwrite=True) :param path: absolute path of the file on the local file system you want to upload :param overwrite: if False, non of the file will be upload / overwritten if any of target s3 location already taken. .. versionadded:: 1.0.1 """self.ensure_object()ifoverwriteisFalse:self.ensure_not_exists(bsm=bsm)p=Path(path)s3_client=resolve_s3_client(context,bsm)returns3_client.upload_file(Filename=p.abspath,Bucket=self.bucket,Key=self.key,**resolve_kwargs(ExtraArgs=extra_args,Callback=callback,Config=config,))
[docs]defupload_dir(self:"S3Path",local_dir:PathType,pattern:str="**/*",overwrite:bool=False,bsm:T.Optional[T.Union["BotoSesManager","S3Client"]]=None,)->int:""" Upload a directory on local file system and all sub-folders, files to a S3 prefix (logical directory) Example:: >>> s3path = S3Path("bucket", "datalake", "orders/") >>> s3path.upload_dir(path="/data/orders", overwrite=True) :param local_dir: absolute path of the directory on the local file system you want to upload :param pattern: linux styled glob pattern match syntax. see this official reference https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob for more details :param overwrite: if False, non of the file will be upload / overwritten if any of target s3 location already taken. :return: number of files uploaded .. versionadded:: 1.0.1 """self.ensure_dir()s3_client=resolve_s3_client(context,bsm)returnupload_dir(s3_client=s3_client,bucket=self.bucket,prefix=self.key,local_dir=local_dir,pattern=pattern,overwrite=overwrite,)