Pulls the contents of an S3 bucket folder to the current working directory.
Parameters:
Name
Type
Description
Default
bucket
str
The name of the S3 bucket where files are stored.
required
folder
str
The folder in the S3 bucket where files are stored.
required
credentials
Optional[Dict]
A dictionary of AWS credentials (aws_access_key_id,
aws_secret_access_key, aws_session_token) or MinIO credentials
(minio_root_user, minio_root_password).
None
client_parameters
Optional[Dict]
A dictionary of additional parameters to pass to the
boto3 client.
defpull_from_s3(bucket:str,folder:str,credentials:Optional[Dict]=None,client_parameters:Optional[Dict]=None,)->PullFromS3Output:""" Pulls the contents of an S3 bucket folder to the current working directory. Args: bucket: The name of the S3 bucket where files are stored. folder: The folder in the S3 bucket where files are stored. credentials: A dictionary of AWS credentials (aws_access_key_id, aws_secret_access_key, aws_session_token) or MinIO credentials (minio_root_user, minio_root_password). client_parameters: A dictionary of additional parameters to pass to the boto3 client. Returns: A dictionary containing the bucket, folder, and local directory where files were downloaded. Examples: Pull files from S3 using the default credentials and client parameters: ```yaml pull: - prefect_aws.deployments.steps.pull_from_s3: requires: prefect-aws bucket: my-bucket folder: my-project ``` Pull files from S3 using credentials stored in a block: ```yaml pull: - prefect_aws.deployments.steps.pull_from_s3: requires: prefect-aws bucket: my-bucket folder: my-project credentials: "{{ prefect.blocks.aws-credentials.dev-credentials }}" ``` """s3=get_s3_client(credentials=credentials,client_parameters=client_parameters)local_path=Path.cwd()paginator=s3.get_paginator("list_objects_v2")forresultinpaginator.paginate(Bucket=bucket,Prefix=folder):forobjinresult.get("Contents",[]):remote_key=obj["Key"]ifremote_key[-1]=="/":# object is a folder and will be created if it contains any objectscontinuetarget=PurePosixPath(local_path/relative_path_to_current_platform(remote_key).relative_to(folder))Path.mkdir(Path(target.parent),parents=True,exist_ok=True)s3.download_file(bucket,remote_key,str(target))return{"bucket":bucket,"folder":folder,"directory":str(local_path),}
Pushes the contents of the current working directory to an S3 bucket,
excluding files and folders specified in the ignore_file.
Parameters:
Name
Type
Description
Default
bucket
str
The name of the S3 bucket where files will be uploaded.
required
folder
str
The folder in the S3 bucket where files will be uploaded.
required
credentials
Optional[Dict]
A dictionary of AWS credentials (aws_access_key_id,
aws_secret_access_key, aws_session_token) or MinIO credentials
(minio_root_user, minio_root_password).
None
client_parameters
Optional[Dict]
A dictionary of additional parameters to pass to the boto3
client.
defpush_to_s3(bucket:str,folder:str,credentials:Optional[Dict]=None,client_parameters:Optional[Dict]=None,ignore_file:Optional[str]=".prefectignore",)->PushToS3Output:""" Pushes the contents of the current working directory to an S3 bucket, excluding files and folders specified in the ignore_file. Args: bucket: The name of the S3 bucket where files will be uploaded. folder: The folder in the S3 bucket where files will be uploaded. credentials: A dictionary of AWS credentials (aws_access_key_id, aws_secret_access_key, aws_session_token) or MinIO credentials (minio_root_user, minio_root_password). client_parameters: A dictionary of additional parameters to pass to the boto3 client. ignore_file: The name of the file containing ignore patterns. Returns: A dictionary containing the bucket and folder where files were uploaded. Examples: Push files to an S3 bucket: ```yaml push: - prefect_aws.deployments.steps.push_to_s3: requires: prefect-aws bucket: my-bucket folder: my-project ``` Push files to an S3 bucket using credentials stored in a block: ```yaml push: - prefect_aws.deployments.steps.push_to_s3: requires: prefect-aws bucket: my-bucket folder: my-project credentials: "{{ prefect.blocks.aws-credentials.dev-credentials }}" ``` """s3=get_s3_client(credentials=credentials,client_parameters=client_parameters)local_path=Path.cwd()included_files=Noneifignore_fileandPath(ignore_file).exists():withopen(ignore_file,"r")asf:ignore_patterns=f.readlines()included_files=filter_files(str(local_path),ignore_patterns)forlocal_file_pathinlocal_path.expanduser().rglob("*"):if(included_filesisnotNoneandstr(local_file_path.relative_to(local_path))notinincluded_files):continueelifnotlocal_file_path.is_dir():remote_file_path=Path(folder)/local_file_path.relative_to(local_path)s3.upload_file(str(local_file_path),bucket,str(remote_file_path.as_posix()))return{"bucket":bucket,"folder":folder,}