Skip to main content

prefect_aws.s3

Tasks for interacting with AWS S3

Functions

get_s3_client

Get a boto3 S3 client with the given credentials and client parameters. Args:
  • credentials: A dictionary of credentials to use for authentication with AWS.
  • client_parameters: A dictionary of parameters to use for the boto3 client initialization.
Returns:
  • A boto3 S3 client.

adownload_from_bucket

Downloads an object with a given key from a given S3 bucket. Added in prefect-aws==0.5.3. Args:
  • bucket: Name of bucket to download object from. Required if a default value was not supplied when creating the task.
  • key: Key of object to download. Required if a default value was not supplied when creating the task.
  • aws_credentials: Credentials to use for authentication with AWS.
  • aws_client_parameters: Custom parameter for the boto3 client initialization.
Returns:
  • A bytes representation of the downloaded object.

download_from_bucket

Downloads an object with a given key from a given S3 bucket. Args:
  • bucket: Name of bucket to download object from. Required if a default value was not supplied when creating the task.
  • key: Key of object to download. Required if a default value was not supplied when creating the task.
  • aws_credentials: Credentials to use for authentication with AWS.
  • aws_client_parameters: Custom parameter for the boto3 client initialization.
Returns:
  • A bytes representation of the downloaded object.

aupload_to_bucket

Asynchronously uploads data to an S3 bucket. Added in prefect-aws==0.5.3. Args:
  • data: Bytes representation of data to upload to S3.
  • bucket: Name of bucket to upload data to. Required if a default value was not supplied when creating the task.
  • aws_credentials: Credentials to use for authentication with AWS.
  • aws_client_parameters: Custom parameter for the boto3 client initialization..
  • key: Key of object to download. Defaults to a UUID string.
Returns:
  • The key of the uploaded object

upload_to_bucket

Uploads data to an S3 bucket. Args:
  • data: Bytes representation of data to upload to S3.
  • bucket: Name of bucket to upload data to. Required if a default value was not supplied when creating the task.
  • aws_credentials: Credentials to use for authentication with AWS.
  • aws_client_parameters: Custom parameter for the boto3 client initialization..
  • key: Key of object to download. Defaults to a UUID string.
Returns:
  • The key of the uploaded object

acopy_objects

Asynchronously uses S3’s internal CopyObject to copy objects within or between buckets. To copy objects between buckets, the credentials must have permission to read the source object and write to the target object. If the credentials do not have those permissions, try using S3Bucket.stream_from. Added in prefect-aws==0.5.3. Args:
  • source_path: The path to the object to copy. Can be a string or Path.
  • target_path: The path to copy the object to. Can be a string or Path.
  • source_bucket_name: The bucket to copy the object from.
  • aws_credentials: Credentials to use for authentication with AWS.
  • target_bucket_name: The bucket to copy the object to. If not provided, defaults to source_bucket.
  • **copy_kwargs: Additional keyword arguments to pass to S3Client.copy_object.
Returns:
  • The path that the object was copied to. Excludes the bucket name.
Examples: Copy notes.txt from s3://my-bucket/my_folder/notes.txt to s3://my-bucket/my_folder/notes_copy.txt.
Copy notes.txt from s3://my-bucket/my_folder/notes.txt to s3://other-bucket/notes_copy.txt.

copy_objects

Uses S3’s internal CopyObject to copy objects within or between buckets. To copy objects between buckets, the credentials must have permission to read the source object and write to the target object. If the credentials do not have those permissions, try using S3Bucket.stream_from. Args:
  • source_path: The path to the object to copy. Can be a string or Path.
  • target_path: The path to copy the object to. Can be a string or Path.
  • source_bucket_name: The bucket to copy the object from.
  • aws_credentials: Credentials to use for authentication with AWS.
  • target_bucket_name: The bucket to copy the object to. If not provided, defaults to source_bucket.
  • **copy_kwargs: Additional keyword arguments to pass to S3Client.copy_object.
Returns:
  • The path that the object was copied to. Excludes the bucket name.
Examples: Copy notes.txt from s3://my-bucket/my_folder/notes.txt to s3://my-bucket/my_folder/notes_copy.txt.
Copy notes.txt from s3://my-bucket/my_folder/notes.txt to s3://other-bucket/notes_copy.txt.

amove_objects

Asynchronously moves an object from one S3 location to another. To move objects between buckets, the credentials must have permission to read and delete the source object and write to the target object. If the credentials do not have those permissions, this method will raise an error. If the credentials have permission to read the source object but not delete it, the object will be copied but not deleted. Added in prefect-aws==0.5.3. Args:
  • source_path: The path of the object to move
  • target_path: The path to move the object to
  • source_bucket_name: The name of the bucket containing the source object
  • aws_credentials: Credentials to use for authentication with AWS.
  • target_bucket_name: The bucket to copy the object to. If not provided, defaults to source_bucket.
Returns:
  • The path that the object was moved to. Excludes the bucket name.

move_objects

Move an object from one S3 location to another. To move objects between buckets, the credentials must have permission to read and delete the source object and write to the target object. If the credentials do not have those permissions, this method will raise an error. If the credentials have permission to read the source object but not delete it, the object will be copied but not deleted. Args:
  • source_path: The path of the object to move
  • target_path: The path to move the object to
  • source_bucket_name: The name of the bucket containing the source object
  • aws_credentials: Credentials to use for authentication with AWS.
  • target_bucket_name: The bucket to copy the object to. If not provided, defaults to source_bucket.
Returns:
  • The path that the object was moved to. Excludes the bucket name.

alist_objects

Asynchronously lists details of objects in a given S3 bucket. Added in prefect-aws==0.5.3. Args:
  • bucket: Name of bucket to list items from. Required if a default value was not supplied when creating the task.
  • aws_credentials: Credentials to use for authentication with AWS.
  • aws_client_parameters: Custom parameter for the boto3 client initialization..
  • prefix: Used to filter objects with keys starting with the specified prefix.
  • delimiter: Character used to group keys of listed objects.
  • page_size: Number of objects to return in each request to the AWS API.
  • max_items: Maximum number of objects that to be returned by task.
  • jmespath_query: Query used to filter objects based on object attributes refer to the boto3 docs for more information on how to construct queries.
Returns:
  • A list of dictionaries containing information about the objects retrieved. Refer to the boto3 docs for an example response.

list_objects

Lists details of objects in a given S3 bucket. Args:
  • bucket: Name of bucket to list items from. Required if a default value was not supplied when creating the task.
  • aws_credentials: Credentials to use for authentication with AWS.
  • aws_client_parameters: Custom parameter for the boto3 client initialization..
  • prefix: Used to filter objects with keys starting with the specified prefix.
  • delimiter: Character used to group keys of listed objects.
  • page_size: Number of objects to return in each request to the AWS API.
  • max_items: Maximum number of objects that to be returned by task.
  • jmespath_query: Query used to filter objects based on object attributes refer to the boto3 docs for more information on how to construct queries.
Returns:
  • A list of dictionaries containing information about the objects retrieved. Refer to the boto3 docs for an example response.

Classes

S3Bucket

Block used to store data using AWS S3 or S3-compatible object storage like MinIO. Attributes:
  • bucket_name: Name of your bucket.
  • credentials: A block containing your credentials to AWS or MinIO.
  • bucket_folder: A default path to a folder within the S3 bucket to use for reading and writing objects.
Methods:

adownload_folder_to_path

Asynchronously downloads objects within a folder (excluding the folder itself) from the S3 bucket to a folder. Args:
  • from_folder: The path to the folder to download from.
  • to_folder: The path to download the folder to.
  • **download_kwargs: Additional keyword arguments to pass to Client.download_file.
Returns:
  • The absolute path that the folder was downloaded to.
Examples: Download my_folder to a local folder named my_folder.

adownload_object_to_file_object

Asynchronously downloads an object from the object storage service to a file-like object, which can be a BytesIO object or a BufferedWriter. Args:
  • from_path: The path to the object to download from; this gets prefixed with the bucket_folder.
  • to_file_object: The file-like object to download the object to.
  • **download_kwargs: Additional keyword arguments to pass to Client.download_fileobj.
Returns:
  • The file-like object that the object was downloaded to.
Examples: Download my_folder/notes.txt object to a BytesIO object.
Download my_folder/notes.txt object to a BufferedWriter.

adownload_object_to_path

Asynchronously downloads an object from the S3 bucket to a path. Args:
  • from_path: The path to the object to download; this gets prefixed with the bucket_folder.
  • to_path: The path to download the object to. If not provided, the object’s name will be used.
  • **download_kwargs: Additional keyword arguments to pass to Client.download_file.
Returns:
  • The absolute path that the object was downloaded to.
Examples: Download my_folder/notes.txt object to notes.txt.

aget_directory

Asynchronously copies a folder from the configured S3 bucket to a local directory. Defaults to copying the entire contents of the block’s basepath to the current working directory. Args:
  • from_path: Path in S3 bucket to download from. Defaults to the block’s configured basepath.
  • local_path: Local path to download S3 contents to. Defaults to the current working directory.

alist_objects

Asynchronously lists objects in the S3 bucket. Args:
  • folder: Folder to list objects from.
  • delimiter: Character used to group keys of listed objects.
  • page_size: Number of objects to return in each request to the AWS API.
  • max_items: Maximum number of objects that to be returned by task.
  • jmespath_query: Query used to filter objects based on object attributes refer to the boto3 docs for more information on how to construct queries.
Returns:
  • List of objects and their metadata in the bucket.
Examples: List objects under the base_folder.

amove_object

Asynchronously uses S3’s internal CopyObject and DeleteObject to move objects within or between buckets. To move objects between buckets, self’s credentials must have permission to read and delete the source object and write to the target object. If the credentials do not have those permissions, this method will raise an error. If the credentials have permission to read the source object but not delete it, the object will be copied but not deleted. Args:
  • from_path: The path of the object to move.
  • to_path: The path to move the object to.
  • to_bucket: The bucket to move to. Defaults to the current bucket.
Returns:
  • The path that the object was moved to. Excludes the bucket name.
Examples: Move notes.txt from my_folder/notes.txt to my_folder/notes_copy.txt.
Move notes.txt from my_folder/notes.txt to my_folder/notes_copy.txt in another bucket.

aput_directory

Asynchronously uploads a directory from a given local path to the configured S3 bucket in a given folder. Defaults to uploading the entire contents the current working directory to the block’s basepath. Args:
  • local_path: Path to local directory to upload from.
  • to_path: Path in S3 bucket to upload to. Defaults to block’s configured basepath.
  • ignore_file: Path to file containing gitignore style expressions for filepaths to ignore.

aread_path

Asynchronously reads the contents of a specified path from the S3 bucket. Provide the entire path to the key in S3. Args:
  • path: Entire path to (and including) the key.

astream_from

Asynchronously streams an object from another bucket to this bucket. Requires the object to be downloaded and uploaded in chunks. If self’s credentials allow for writes to the other bucket, try using S3Bucket.copy_object. Added in version 0.5.3. Args:
  • bucket: The bucket to stream from.
  • from_path: The path of the object to stream.
  • to_path: The path to stream the object to. Defaults to the object’s name.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload_fileobj.
Returns:
  • The path that the object was uploaded to.
Examples: Stream notes.txt from your-bucket/notes.txt to my-bucket/landed/notes.txt.

aupload_from_file_object

Asynchronously uploads an object to the S3 bucket from a file-like object, which can be a BytesIO object or a BufferedReader. Args:
  • from_file_object: The file-like object to upload from.
  • to_path: The path to upload the object to.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload_fileobj.
Returns:
  • The path that the object was uploaded to.
Examples: Upload BytesIO object to my_folder/notes.txt.
Upload BufferedReader object to my_folder/notes.txt.

aupload_from_folder

Asynchronously uploads files within a folder (excluding the folder itself) to the object storage service folder. Added in version prefect-aws==0.5.3. Args:
  • from_folder: The path to the folder to upload from.
  • to_folder: The path to upload the folder to.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload_fileobj.
Returns:
  • The path that the folder was uploaded to.
Examples: Upload contents from my_folder to new_folder.

aupload_from_path

Asynchronously uploads an object from a path to the S3 bucket. Added in version 0.5.3. Args:
  • from_path: The path to the file to upload from.
  • to_path: The path to upload the file to.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload.
Returns:
  • The path that the object was uploaded to.
Examples: Upload notes.txt to my_folder/notes.txt.

awrite_path

Asynchronously writes to an S3 bucket. Args: path: The key name. Each object in your bucket has a unique key (or key name). content: What you are uploading to S3. Example: Write data to the path dogs/small_dogs/havanese in an S3 Bucket:

basepath

The base path of the S3 bucket. Returns:
  • The base path of the S3 bucket.

basepath

copy_object

Uses S3’s internal CopyObject to copy objects within or between buckets. To copy objects between buckets, self’s credentials must have permission to read the source object and write to the target object. If the credentials do not have those permissions, try using S3Bucket.stream_from. Args:
  • from_path: The path of the object to copy.
  • to_path: The path to copy the object to.
  • to_bucket: The bucket to copy to. Defaults to the current bucket.
  • **copy_kwargs: Additional keyword arguments to pass to S3Client.copy_object.
Returns:
  • The path that the object was copied to. Excludes the bucket name.
Examples: Copy notes.txt from my_folder/notes.txt to my_folder/notes_copy.txt.
Copy notes.txt from my_folder/notes.txt to my_folder/notes_copy.txt in another bucket.

download_folder_to_path

Downloads objects within a folder (excluding the folder itself) from the S3 bucket to a folder. Changed in version 0.6.0. Args:
  • from_folder: The path to the folder to download from.
  • to_folder: The path to download the folder to.
  • **download_kwargs: Additional keyword arguments to pass to Client.download_file.
Returns:
  • The absolute path that the folder was downloaded to.
Examples: Download my_folder to a local folder named my_folder.

download_object_to_file_object

Downloads an object from the object storage service to a file-like object, which can be a BytesIO object or a BufferedWriter. Args:
  • from_path: The path to the object to download from; this gets prefixed with the bucket_folder.
  • to_file_object: The file-like object to download the object to.
  • **download_kwargs: Additional keyword arguments to pass to Client.download_fileobj.
Returns:
  • The file-like object that the object was downloaded to.
Examples: Download my_folder/notes.txt object to a BytesIO object.
Download my_folder/notes.txt object to a BufferedWriter.

download_object_to_path

Downloads an object from the S3 bucket to a path. Args:
  • from_path: The path to the object to download; this gets prefixed with the bucket_folder.
  • to_path: The path to download the object to. If not provided, the object’s name will be used.
  • **download_kwargs: Additional keyword arguments to pass to Client.download_file.
Returns:
  • The absolute path that the object was downloaded to.
Examples: Download my_folder/notes.txt object to notes.txt.

get_directory

Copies a folder from the configured S3 bucket to a local directory. Defaults to copying the entire contents of the block’s basepath to the current working directory. Args:
  • from_path: Path in S3 bucket to download from. Defaults to the block’s configured basepath.
  • local_path: Local path to download S3 contents to. Defaults to the current working directory.

list_objects

Args:
  • folder: Folder to list objects from.
  • delimiter: Character used to group keys of listed objects.
  • page_size: Number of objects to return in each request to the AWS API.
  • max_items: Maximum number of objects that to be returned by task.
  • jmespath_query: Query used to filter objects based on object attributes refer to the boto3 docs for more information on how to construct queries.
Returns:
  • List of objects and their metadata in the bucket.
Examples: List objects under the base_folder.

move_object

Uses S3’s internal CopyObject and DeleteObject to move objects within or between buckets. To move objects between buckets, self’s credentials must have permission to read and delete the source object and write to the target object. If the credentials do not have those permissions, this method will raise an error. If the credentials have permission to read the source object but not delete it, the object will be copied but not deleted. Args:
  • from_path: The path of the object to move.
  • to_path: The path to move the object to.
  • to_bucket: The bucket to move to. Defaults to the current bucket.
Returns:
  • The path that the object was moved to. Excludes the bucket name.
Examples: Move notes.txt from my_folder/notes.txt to my_folder/notes_copy.txt.
Move notes.txt from my_folder/notes.txt to my_folder/notes_copy.txt in another bucket.

put_directory

Uploads a directory from a given local path to the configured S3 bucket in a given folder. Defaults to uploading the entire contents the current working directory to the block’s basepath. Args:
  • local_path: Path to local directory to upload from.
  • to_path: Path in S3 bucket to upload to. Defaults to block’s configured basepath.
  • ignore_file: Path to file containing gitignore style expressions for filepaths to ignore.

read_path

Read specified path from S3 and return contents. Provide the entire path to the key in S3. Args:
  • path: Entire path to (and including) the key.

stream_from

Streams an object from another bucket to this bucket. Requires the object to be downloaded and uploaded in chunks. If self’s credentials allow for writes to the other bucket, try using S3Bucket.copy_object. Args:
  • bucket: The bucket to stream from.
  • from_path: The path of the object to stream.
  • to_path: The path to stream the object to. Defaults to the object’s name.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload_fileobj.
Returns:
  • The path that the object was uploaded to.
Examples: Stream notes.txt from your-bucket/notes.txt to my-bucket/landed/notes.txt.

upload_from_file_object

Uploads an object to the S3 bucket from a file-like object, which can be a BytesIO object or a BufferedReader. Args:
  • from_file_object: The file-like object to upload from.
  • to_path: The path to upload the object to.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload_fileobj.
Returns:
  • The path that the object was uploaded to.
Examples: Upload BytesIO object to my_folder/notes.txt.
Upload BufferedReader object to my_folder/notes.txt.

upload_from_folder

Uploads files within a folder (excluding the folder itself) to the object storage service folder. Args:
  • from_folder: The path to the folder to upload from.
  • to_folder: The path to upload the folder to.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload_fileobj.
Returns:
  • The path that the folder was uploaded to.
Examples: Upload contents from my_folder to new_folder.

upload_from_path

Uploads an object from a path to the S3 bucket. Args:
  • from_path: The path to the file to upload from.
  • to_path: The path to upload the file to.
  • **upload_kwargs: Additional keyword arguments to pass to Client.upload.
Returns:
  • The path that the object was uploaded to.
Examples: Upload notes.txt to my_folder/notes.txt.

validate_credentials

write_path

Writes to an S3 bucket. Args: path: The key name. Each object in your bucket has a unique key (or key name). content: What you are uploading to S3. Example: Write data to the path dogs/small_dogs/havanese in an S3 Bucket: