Google Cloud Storage 到 Google Drive 传输操作符

Google 有两个用于存储数据的服务。 Google Cloud Storage 用于存储来自各种应用的大型数据。 Google Drive 用于存储日常使用的数据,包括文档和照片。 Google Cloud Storage 与 Google Cloud 服务紧密集成。 Google Drive 具有内置机制以促进团队协作,例如文档编辑器、文件共享机制。

前置任务

要使用这些操作符,您必须执行以下几个步骤

操作符

Google Storage 和 Google Drive 之间的文件传输使用 GCSToGoogleDriveOperator 操作符执行。

您可以使用 Jinja 模板source_bucket, source_object, destination_object, impersonation_chain 参数一起使用,这使您可以动态确定值。

复制单个文件

以下操作符将复制单个文件。

tests/system/google/cloud/gcs/example_gcs_to_gdrive.py

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}",
)

复制到现有文件夹

以下操作符将把单个文件复制到具有指定 ID 的现有文件夹中。

tests/system/google/cloud/gcs/example_gcs_to_gdrive.py

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,
)

复制多个文件

以下操作符将复制所有多个文件(即使用通配符)。

tests/system/google/cloud/gcs/example_gcs_to_gdrive.py

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_object 参数可以移动文件。 将文件复制到 Google Drive 后,会删除存储桶中的原始文件。

tests/system/google/cloud/gcs/example_gcs_to_gdrive.py

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,
)

参考

欲了解更多信息,请参阅

此条目是否有帮助?