Skip to main content

prefect_gcp.cloud_storage

Tasks for interacting with GCP Cloud Storage.

Functions

acloud_storage_create_bucket

Creates a bucket (async version). Args:
  • bucket: Name of the bucket.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • location: Location of the bucket.
  • **create_kwargs: Additional keyword arguments to pass to client.create_bucket.
Returns:
  • The bucket name.

cloud_storage_create_bucket

Creates a bucket. Args:
  • bucket: Name of the bucket.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • location: Location of the bucket.
  • **create_kwargs: Additional keyword arguments to pass to client.create_bucket.
Returns:
  • The bucket name.

acloud_storage_download_blob_as_bytes

Downloads a blob as bytes (async version). Args:
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_as_bytes.
Returns:
  • A bytes or string representation of the blob object.

cloud_storage_download_blob_as_bytes

Downloads a blob as bytes. Args:
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_as_bytes.
Returns:
  • A bytes or string representation of the blob object.

acloud_storage_download_blob_to_file

Downloads a blob to a file path (async version). Args:
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • path: Downloads the contents to the provided file path; if the path is a directory, automatically joins the blob name.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_filename.
Returns:
  • The path to the blob object.

cloud_storage_download_blob_to_file

Downloads a blob to a file path. Args:
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • path: Downloads the contents to the provided file path; if the path is a directory, automatically joins the blob name.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_filename.
Returns:
  • The path to the blob object.

acloud_storage_upload_blob_from_string

Uploads a blob from a string or bytes representation of data (async version). Args:
  • data: String or bytes representation of data to upload.
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • content_type: Type of content being uploaded.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_string.
Returns:
  • The blob name.

cloud_storage_upload_blob_from_string

Uploads a blob from a string or bytes representation of data. Args:
  • data: String or bytes representation of data to upload.
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • content_type: Type of content being uploaded.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_string.
Returns:
  • The blob name.

acloud_storage_upload_blob_from_file

Uploads a blob from file path or file-like object (async version). Usage for passing in file-like object is if the data was downloaded from the web; can bypass writing to disk and directly upload to Cloud Storage. Args:
  • file: Path to data or file like object to upload.
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • content_type: Type of content being uploaded.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_file or Blob.upload_from_filename.
Returns:
  • The blob name.

cloud_storage_upload_blob_from_file

Uploads a blob from file path or file-like object. Usage for passing in file-like object is if the data was downloaded from the web; can bypass writing to disk and directly upload to Cloud Storage. Args:
  • file: Path to data or file like object to upload.
  • bucket: Name of the bucket.
  • blob: Name of the Cloud Storage blob.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • content_type: Type of content being uploaded.
  • chunk_size: The size of a chunk of data whenever iterating (in bytes). This must be a multiple of 256 KB per the API specification.
  • encryption_key: An encryption key.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_file or Blob.upload_from_filename.
Returns:
  • The blob name.

cloud_storage_copy_blob

Copies data from one Google Cloud Storage bucket to another, without downloading it locally. Args:
  • source_bucket: Source bucket name.
  • dest_bucket: Destination bucket name.
  • source_blob: Source blob name.
  • gcp_credentials: Credentials to use for authentication with GCP.
  • dest_blob: Destination blob name; if not provided, defaults to source_blob.
  • timeout: The number of seconds the transport should wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout).
  • project: Name of the project to use; overrides the gcp_credentials project if provided.
  • **copy_kwargs: Additional keyword arguments to pass to Bucket.copy_blob.
Returns:
  • Destination blob name.

Classes

DataFrameSerializationFormat

An enumeration class to represent different file formats, compression options for upload_from_dataframe Attributes:
  • CSV: Representation for ‘csv’ file format with no compression and its related content type and suffix.
  • CSV_GZIP: Representation for ‘csv’ file format with ‘gzip’ compression and its related content type and suffix.
  • PARQUET: Representation for ‘parquet’ file format with no compression and its related content type and suffix.
  • PARQUET_SNAPPY: Representation for ‘parquet’ file format with ‘snappy’ compression and its related content type and suffix.
  • PARQUET_GZIP: Representation for ‘parquet’ file format with ‘gzip’ compression and its related content type and suffix.
Methods:

compression

The compression type of the current instance.

content_type

The content type of the current instance.

fix_extension_with

Fix the extension of a GCS blob. Args:
  • gcs_blob_path: The path to the GCS blob to be modified.
Returns:
  • The modified path to the GCS blob with the new extension.

format

The file format of the current instance.

suffix

The suffix of the file format of the current instance.

GcsBucket

Block used to store data using GCP Cloud Storage Buckets. Note! GcsBucket in prefect-gcp is a unique block, separate from GCS in core Prefect. GcsBucket does not use gcsfs under the hood, instead using the google-cloud-storage package, and offers more configuration and functionality. Attributes:
  • bucket: Name of the bucket.
  • gcp_credentials: The credentials to authenticate with GCP.
  • bucket_folder: A default path to a folder within the GCS bucket to use for reading and writing objects.
Methods:

acreate_bucket

Creates a bucket (async version). Args:
  • location: The location of the bucket.
  • **create_kwargs: Additional keyword arguments to pass to the create_bucket method.
Returns:
  • The bucket object.
Examples: Create a bucket.

adownload_folder_to_path

Downloads objects within a folder (excluding the folder itself) from the object storage service to a folder (async version). Args:
  • from_folder: The path to the folder to download from; this gets prefixed with the bucket_folder.
  • to_folder: The path to download the folder to. If not provided, will default to the current directory.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_filename.
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

Downloads an object from the object storage service to a file-like object (async version), which can be a BytesIO object or a BufferedWriter. Args:
  • from_path: The path to the blob to download from; this gets prefixed with the bucket_folder.
  • to_file_object: The file-like object to download the blob to.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_file.
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

Downloads an object from the object storage service to a path (async version). Args:
  • from_path: The path to the blob to download; this gets prefixed with the bucket_folder.
  • to_path: The path to download the blob to. If not provided, the blob’s name will be used.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_filename.
Returns:
  • The absolute path that the object was downloaded to.
Examples: Download my_folder/notes.txt object to notes.txt.

aget_bucket

Returns the bucket object (async version). Returns:
  • The bucket object.
Examples: Get the bucket object.

aget_directory

Copies a folder from the configured GCS bucket to a local directory (async version). Defaults to copying the entire contents of the block’s bucket_folder to the current working directory. Args:
  • from_path: Path in GCS bucket to download from. Defaults to the block’s configured bucket_folder.
  • local_path: Local path to download GCS bucket contents to. Defaults to the current working directory.
Returns:
  • A list of downloaded file paths.

alist_blobs

Lists all blobs in the bucket that are in a folder (async version). Folders are not included in the output. Args:
  • folder: The folder to list blobs from.
Returns:
  • A list of Blob objects.
Examples: Get all blobs from a folder named “prefect”.

alist_folders

Lists all folders and subfolders in the bucket (async version). Args:
  • folder: List all folders and subfolders inside given folder.
Returns:
  • A list of folders.
Examples: Get all folders from a bucket named “my-bucket”.
Get all folders from a folder called years

aput_directory

Uploads a directory from a given local path to the configured GCS bucket in a given folder (async version). Defaults to uploading the entire contents the current working directory to the block’s bucket_folder. Args:
  • local_path: Path to local directory to upload from.
  • to_path: Path in GCS bucket to upload to. Defaults to block’s configured bucket_folder.
  • ignore_file: Path to file containing gitignore style expressions for filepaths to ignore.
Returns:
  • The number of files uploaded.

aread_path

Read specified path from GCS and return contents (async version). Provide the entire path to the key in GCS. Args:
  • path: Entire path to (and including) the key.
Returns:
  • A bytes or string representation of the blob object.

aupload_from_dataframe

Upload a Pandas DataFrame to Google Cloud Storage in various formats (async version). This function uploads the data in a Pandas DataFrame to Google Cloud Storage in a specified format, such as .csv, .csv.gz, .parquet, .parquet.snappy, and .parquet.gz. Args:
  • df: The Pandas DataFrame to be uploaded.
  • to_path: The destination path for the uploaded DataFrame.
  • serialization_format: The format to serialize the DataFrame into. When passed as a str, the valid options are: ‘csv’, ‘csv_gzip’, ‘parquet’, ‘parquet_snappy’, ‘parquet_gzip’. Defaults to DataFrameSerializationFormat.CSV_GZIP.
  • **upload_kwargs: Additional keyword arguments to pass to the underlying upload_from_dataframe method.
Returns:
  • The path that the object was uploaded to.

aupload_from_file_object

Uploads an object to the object storage service from a file-like object (async version), 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; this gets prefixed with the bucket_folder.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_file.
Returns:
  • The path that the object was uploaded to.
Examples: Upload my_folder/notes.txt object to a BytesIO object.
Upload BufferedReader object to my_folder/notes.txt.

aupload_from_folder

Uploads files within a folder (excluding the folder itself) to the object storage service folder (async version). Args:
  • from_folder: The path to the folder to upload from.
  • to_folder: The path to upload the folder to. If not provided, will default to bucket_folder or the base directory of the bucket.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_filename.
Returns:
  • The path that the folder was uploaded to.
Examples: Upload local folder my_folder to the bucket’s folder my_folder.

aupload_from_path

Uploads an object from a path to the object storage service (async version). Args:
  • from_path: The path to the file to upload from.
  • to_path: The path to upload the file to. If not provided, will use the file name of from_path; this gets prefixed with the bucket_folder.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_filename.
Returns:
  • The path that the object was uploaded to.
Examples: Upload notes.txt to my_folder/notes.txt.

awrite_path

Writes to an GCS bucket (async version). Args:
  • path: The key name. Each object in your bucket has a unique key (or key name).
  • content: What you are uploading to GCS Bucket.
Returns:
  • The path that the contents were written to.

basepath

Read-only property that mirrors the bucket folder. Used for deployment.

create_bucket

Creates a bucket. Args:
  • location: The location of the bucket.
  • **create_kwargs: Additional keyword arguments to pass to the create_bucket method.
Returns:
  • The bucket object.
Examples: Create a bucket.

download_folder_to_path

Downloads objects within a folder (excluding the folder itself) from the object storage service to a folder. Args:
  • from_folder: The path to the folder to download from; this gets prefixed with the bucket_folder.
  • to_folder: The path to download the folder to. If not provided, will default to the current directory.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_filename.
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 blob to download from; this gets prefixed with the bucket_folder.
  • to_file_object: The file-like object to download the blob to.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_file.
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 object storage service to a path. Args:
  • from_path: The path to the blob to download; this gets prefixed with the bucket_folder.
  • to_path: The path to download the blob to. If not provided, the blob’s name will be used.
  • **download_kwargs: Additional keyword arguments to pass to Blob.download_to_filename.
Returns:
  • The absolute path that the object was downloaded to.
Examples: Download my_folder/notes.txt object to notes.txt.

get_bucket

Returns the bucket object. Returns:
  • The bucket object.
Examples: Get the bucket object.

get_directory

Copies a folder from the configured GCS bucket to a local directory. Defaults to copying the entire contents of the block’s bucket_folder to the current working directory. Args:
  • from_path: Path in GCS bucket to download from. Defaults to the block’s configured bucket_folder.
  • local_path: Local path to download GCS bucket contents to. Defaults to the current working directory.
Returns:
  • A list of downloaded file paths.

list_blobs

Lists all blobs in the bucket that are in a folder. Folders are not included in the output. Args:
  • folder: The folder to list blobs from.
Returns:
  • A list of Blob objects.
Examples: Get all blobs from a folder named “prefect”.

list_folders

Lists all folders and subfolders in the bucket. Args:
  • folder: List all folders and subfolders inside given folder.
Returns:
  • A list of folders.
Examples: Get all folders from a bucket named “my-bucket”.
Get all folders from a folder called years

put_directory

Uploads a directory from a given local path to the configured GCS bucket in a given folder. Defaults to uploading the entire contents the current working directory to the block’s bucket_folder. Args:
  • local_path: Path to local directory to upload from.
  • to_path: Path in GCS bucket to upload to. Defaults to block’s configured bucket_folder.
  • ignore_file: Path to file containing gitignore style expressions for filepaths to ignore.
Returns:
  • The number of files uploaded.

read_path

Read specified path from GCS and return contents. Provide the entire path to the key in GCS. Args:
  • path: Entire path to (and including) the key.
Returns:
  • A bytes or string representation of the blob object.

upload_from_dataframe

Upload a Pandas DataFrame to Google Cloud Storage in various formats. This function uploads the data in a Pandas DataFrame to Google Cloud Storage in a specified format, such as .csv, .csv.gz, .parquet, .parquet.snappy, and .parquet.gz. Args:
  • df: The Pandas DataFrame to be uploaded.
  • to_path: The destination path for the uploaded DataFrame.
  • serialization_format: The format to serialize the DataFrame into. When passed as a str, the valid options are: ‘csv’, ‘csv_gzip’, ‘parquet’, ‘parquet_snappy’, ‘parquet_gzip’. Defaults to DataFrameSerializationFormat.CSV_GZIP.
  • **upload_kwargs: Additional keyword arguments to pass to the underlying upload_from_dataframe method.
Returns:
  • The path that the object was uploaded to.

upload_from_file_object

Uploads an object to the object storage service 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; this gets prefixed with the bucket_folder.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_file.
Returns:
  • The path that the object was uploaded to.
Examples: Upload my_folder/notes.txt object to a BytesIO object.
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. If not provided, will default to bucket_folder or the base directory of the bucket.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_filename.
Returns:
  • The path that the folder was uploaded to.
Examples: Upload local folder my_folder to the bucket’s folder my_folder.

upload_from_path

Uploads an object from a path to the object storage service. Args:
  • from_path: The path to the file to upload from.
  • to_path: The path to upload the file to. If not provided, will use the file name of from_path; this gets prefixed with the bucket_folder.
  • **upload_kwargs: Additional keyword arguments to pass to Blob.upload_from_filename.
Returns:
  • The path that the object was uploaded to.
Examples: Upload notes.txt to my_folder/notes.txt.

write_path

Writes to an GCS bucket. Args:
  • path: The key name. Each object in your bucket has a unique key (or key name).
  • content: What you are uploading to GCS Bucket.
Returns:
  • The path that the contents were written to.