如何定时将ADLS容器中的文件迁移/复制至GCS存储桶?
从ADLS定时复制文件到GCS的可行方案
1. 云原生ETL服务(Azure Data Factory + GCS连接器)
- 利用ADF的GCS连接器直接建立与GCS存储桶的连接,无需额外中转环节。
- 创建复制数据活动,配置源为目标ADLS容器,目标为GCS存储桶,可按需设置文件过滤规则(如按修改时间、文件名前缀筛选)。
- 通过ADF的触发器设定时间间隔(如每小时、每天),自动触发复制任务,还支持增量复制(仅同步新增/修改的文件)。
- 优势:全托管服务,无需维护服务器,自带监控和日志功能,操作门槛低。
2. 命令行工具+定时任务
- 使用Azure官方工具
azcopy从ADLS拉取文件到本地临时存储,再通过Google的gsutil上传至GCS。 - 编写Shell/PowerShell脚本实现完整流程,示例逻辑:
# 从ADLS递归复制指定格式文件到临时目录 azcopy copy "https://<storage-account>.dfs.core.windows.net/<container>/<path>" "/tmp/adls-temp" --recursive --include-pattern="*.parquet" # 上传文件到GCS目标路径 gsutil cp -r /tmp/adls-temp/* gs://<gcs-bucket>/<target-path> # 清理临时文件 rm -rf /tmp/adls-temp/* - 借助Linux的
cron或Windows任务计划程序,按指定时间间隔执行脚本。 - 优势:轻量灵活,适合简单同步场景,学习成本低;缺点:需维护运行脚本的服务器,要提前配置
azcopy和gsutil的身份凭据。
3. Apache Airflow 编排同步任务
- 搭建Airflow环境(可部署在Azure VM、GCP Compute Engine或K8s集群)。
- 使用Airflow的
WasbHook连接ADLS,通过LocalFilesystemToGCSOperator完成文件上传,编写DAG定义调度规则,示例片段:from airflow import DAG from airflow.providers.google.cloud.transfers.local_to_gcs import LocalFilesystemToGCSOperator from airflow.providers.microsoft.azure.hooks.wasb import WasbHook from airflow.operators.python import PythonOperator from datetime import datetime, timedelta import os default_args = { 'owner': 'data-team', 'start_date': datetime(2024, 1, 1), 'retries': 1, 'retry_delay': timedelta(minutes=5) } with DAG('adls_gcs_sync_dag', default_args=default_args, schedule_interval='@daily', # 每日执行 catchup=False) as dag: def download_from_adls(): hook = WasbHook(wasb_conn_id='adls_connection') hook.download_file(container_name='source-container', blob_name='daily-data/', local_path='/tmp/adls-sync/', recursive=True) download_task = PythonOperator( task_id='pull_adls_files', python_callable=download_from_adls ) upload_to_gcs = LocalFilesystemToGCSOperator( task_id='push_to_gcs', src='/tmp/adls-sync/*', dst='synced-data/', bucket='target-gcs-bucket', gcp_conn_id='gcs_connection' ) download_task >> upload_to_gcs - 优势:高度可定制,支持复杂任务依赖编排,适配多数据源同步场景;缺点:需自行维护Airflow集群,有一定学习成本。
4. Google Cloud Transfer Service
- 利用Google Cloud Transfer Service,直接将ADLS作为源存储配置定时迁移任务(ADLS Gen2兼容Blob存储协议)。
- 在GCP控制台创建传输任务,选择Azure Blob Storage作为源,填写ADLS存储账户信息、容器路径,目标设为GCS存储桶。
- 设置调度频率(如每日、每周),支持增量同步(仅复制新文件或修改过的文件)。
- 优势:全托管无代码,自动处理身份验证和错误重试;缺点:配置灵活性有限,适合标准化批量迁移场景。
内容的提问来源于stack exchange,提问作者Soumik Das
相关产品推荐
相关产品推荐

