Google Cloud Storage Transfer Operator 到 SFTP¶
Google 有一項服務 Google Cloud Storage。此服務用於儲存來自各種應用程式的大量資料。SFTP (SSH File Transfer Protocol) 是一種安全檔案傳輸協定。它透過 SSH 協定運行。它支援 SSH 的完整安全性和驗證功能。
先決條件任務¶
要使用這些運算子,您必須執行以下幾項操作
使用 Cloud Console 選擇或建立 Cloud Platform 專案。
為您的專案啟用計費功能,如 Google Cloud 文件中所述。
啟用 API,如 Cloud Console 文件中所述。
透過 pip 安裝 API 程式庫。
pip install 'apache-airflow[google]'詳細資訊請參閱 安裝。
運算子¶
SFTP 和 Google Storage 之間的檔案傳輸是透過 GCSToSFTPOperator
運算子執行。
使用 Jinja 模板 以及 source_bucket
、 source_object
、 destination_path
、 impersonation_chain
來動態定義值。
複製單一檔案¶
以下運算子複製單一檔案。
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 伺服器上檔案的完整路徑。
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
來複製目錄。
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
定義了所有複製檔案都會加上前綴的路徑。
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,
)