Google Cloud Storage 到 SFTP 的傳輸運算子¶
Google 提供了一項服務 Google Cloud Storage。此服務用於儲存來自各種應用程式的大量資料。SFTP(SSH 檔案傳輸協議)是一種安全的檔案傳輸協議。它執行在 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 來動態定義值。
複製單個檔案¶
以下運算子複製單個檔案。
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,
)
參考¶
更多資訊,請查閱: