Google Cloud Storage to Google Drive Transfer Operators¶
Google has two services that store data. The Google Cloud Storage is used to store large data from various applications. The Google Drive is used to store daily use data, including documents and photos. Google Cloud Storage has strong integration with Google Cloud services. Google Drive has built-in mechanisms to facilitate group work e.g. document editor, file sharing mechanisms.
Prerequisite Tasks¶
To use these operators, you must do a few things:
Select or create a Cloud Platform project using the Cloud Console.
Enable billing for your project, as described in the Google Cloud documentation.
Enable the API, as described in the Cloud Console documentation.
Install API libraries via pip.
pip install 'apache-airflow[google]'Detailed information is available for Installation.
Operator¶
Transfer files between Google Storage and Google Drive is performed with the
GCSToGoogleDriveOperator
operator.
You can use Jinja templating with
source_bucket
, source_object
, destination_object
, impersonation_chain
parameters which allows you to dynamically determine values.
Copy single files¶
The following Operator would copy a single file.
copy_single_file = GCSToGoogleDriveOperator(
task_id="copy_single_file",
gcp_conn_id=CONNECTION_ID,
source_bucket=BUCKET_NAME,
source_object=f"{TMP_PATH}/{FILE_NAME}",
destination_object=f"{WORK_DIR}/copied_{FILE_NAME}",
)
Copy into an existing folder¶
The following Operator would copy a single file into an existing folder with the specified ID.
copy_single_file_into_folder = GCSToGoogleDriveOperator(
task_id="copy_single_file_into_folder",
gcp_conn_id=CONNECTION_ID,
source_bucket=BUCKET_NAME,
source_object=f"{TMP_PATH}/{FILE_NAME}",
destination_object=f"{WORK_DIR}/copied_{FILE_NAME}",
destination_folder_id=FOLDER_ID,
)
Copy multiple files¶
The following Operator would copy all the multiples files (i.e. using wildcard).
copy_files = GCSToGoogleDriveOperator(
task_id="copy_files",
gcp_conn_id=CONNECTION_ID,
source_bucket=BUCKET_NAME,
source_object=f"{TMP_PATH}/*",
destination_object=f"{WORK_DIR}/",
)
Move files¶
Using the move_object
parameter allows you to move the files. After copying the file to Google Drive,
the original file from the bucket is deleted.
move_files = GCSToGoogleDriveOperator(
task_id="move_files",
gcp_conn_id=CONNECTION_ID,
source_bucket=BUCKET_NAME,
source_object=f"{TMP_PATH}/*.txt",
destination_object=f"{WORK_DIR}/",
move_object=True,
)
Reference¶
For further information, look at: