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,
)
参考¶
更多信息,请查阅: