Google Cloud Storage Transfer Operator 到 SFTP

Google 有一項服務 Google Cloud Storage。此服務用於儲存來自各種應用程式的大量資料。SFTP (SSH File Transfer Protocol) 是一種安全檔案傳輸協定。它透過 SSH 協定運行。它支援 SSH 的完整安全性和驗證功能。

先決條件任務

要使用這些運算子,您必須執行以下幾項操作

運算子

SFTP 和 Google Storage 之間的檔案傳輸是透過 GCSToSFTPOperator 運算子執行。

使用 Jinja 模板 以及 source_bucketsource_objectdestination_pathimpersonation_chain 來動態定義值。

複製單一檔案

以下運算子複製單一檔案。

tests/system/google/cloud/transfers/example_gcs_to_sftp.py[原始碼]

copy_file_from_gcs_to_sftp = GCSToSFTPOperator(
    task_id="file-copy-gsc-to-sftp",
    sftp_conn_id=SFTP_CONN_ID,
    source_bucket=BUCKET_NAME,
    source_object=GCS_SRC_FILE,
    destination_path=DESTINATION_PATH_1,
)

移動單一檔案

要移動檔案,請使用 move_object 參數。一旦檔案複製到 SFTP,Google Storage 中的原始檔案將被刪除。destination_path 參數定義 SFTP 伺服器上檔案的完整路徑。

tests/system/google/cloud/transfers/example_gcs_to_sftp.py[原始碼]

move_file_from_gcs_to_sftp = GCSToSFTPOperator(
    task_id="file-move-gsc-to-sftp",
    sftp_conn_id=SFTP_CONN_ID,
    source_bucket=BUCKET_NAME,
    source_object=GCS_SRC_FILE_IN_DIR,
    destination_path=DESTINATION_PATH_1,
    move_object=True,
)

複製目錄

source_path 參數中使用 wildcard 來複製目錄。

tests/system/google/cloud/transfers/example_gcs_to_sftp.py[原始碼]

copy_dir_from_gcs_to_sftp = GCSToSFTPOperator(
    task_id="dir-copy-gsc-to-sftp",
    sftp_conn_id=SFTP_CONN_ID,
    source_bucket=BUCKET_NAME,
    source_object=GCS_SRC_DIR,
    destination_path=DESTINATION_PATH_2,
)

移動特定檔案

source_path 參數中使用 wildcard 來移動特定檔案。destination_path 定義了所有複製檔案都會加上前綴的路徑。

tests/system/google/cloud/transfers/example_gcs_to_sftp.py[原始碼]

move_dir_from_gcs_to_sftp = GCSToSFTPOperator(
    task_id="dir-move-gsc-to-sftp",
    sftp_conn_id=SFTP_CONN_ID,
    source_bucket=BUCKET_NAME,
    source_object=GCS_SRC_DIR,
    destination_path=DESTINATION_PATH_3,
    keep_directory_structure=False,
)

參考

如需更多資訊,請參閱

此條目是否有幫助?