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

參考

欲瞭解更多資訊,請參閱

此條目是否有幫助?