This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_transfer_and_return_files.py
50 lines (41 loc) · 1.82 KB
/
example_transfer_and_return_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
from datetime import datetime
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.providers.amazon.aws.operators.s3 import S3ListOperator
from universal_transfer_operator.datasets.file.base import File
from universal_transfer_operator.universal_transfer_operator import UniversalTransferOperator
s3_bucket_name = os.getenv("S3_BUCKET_NAME", "astro-sdk-test")
s3_bucket = os.getenv("S3_BUCKET", "s3://astro-sdk-test")
gcs_bucket = os.getenv("GCS_BUCKET", "gs://uto-test")
s3_bucket_prefix = os.getenv("S3_BUCKET_PREFIX", "uto")
gcs_bucket_prefix = os.getenv("GCS_BUCKET_PREFIX", "uto")
def calculate_the_file_transferred(ti):
"""Return the number of files transferred."""
transferred_files = ti.xcom_pull(key="return_value", task_ids=["transfer_non_native_s3_to_gs"])
listed_file_in_s3 = ti.xcom_pull(key="return_value", task_ids=["list_s3_files"])
assert len(transferred_files[0]) == len(listed_file_in_s3[0])
with DAG(
"example_transfers_and_return_files",
schedule_interval=None,
start_date=datetime(2022, 1, 1),
catchup=False,
) as dag:
transfer_non_native_s3_to_gs = UniversalTransferOperator(
task_id="transfer_non_native_s3_to_gs",
source_dataset=File(path=f"{s3_bucket}/{s3_bucket_prefix}/", conn_id="aws_default"),
destination_dataset=File(
path=f"{gcs_bucket}/{gcs_bucket_prefix}/",
conn_id="google_cloud_default",
),
)
list_s3_files = S3ListOperator(
task_id="list_s3_files",
bucket=s3_bucket_name,
prefix=f"{s3_bucket_prefix}/",
aws_conn_id="aws_default",
)
files_transferred = PythonOperator(
task_id="files_transferred", python_callable=calculate_the_file_transferred
)
transfer_non_native_s3_to_gs >> list_s3_files >> files_transferred