2828import logging
2929import os
3030from datetime import datetime
31+ from functools import cache
3132from pathlib import Path
3233from typing import Any
3334
4142from airflow .models .dag import DAG
4243from airflow .providers .google .cloud .operators .gcs import GCSCreateBucketOperator , GCSDeleteBucketOperator
4344from airflow .providers .google .cloud .transfers .gcs_to_gcs import GCSToGCSOperator
45+ from airflow .providers .google .common .utils .get_secret import get_secret
4446from airflow .providers .google .suite .hooks .drive import GoogleDriveHook
4547from airflow .providers .google .suite .transfers .gcs_to_gdrive import GCSToGoogleDriveOperator
4648
7173LOCAL_PATH = str (Path ("gcs" ))
7274FILE_LOCAL_PATH = str (Path (LOCAL_PATH ))
7375FILE_NAME = "example_upload.txt"
76+ GDRIVE_SECRET_ID = "gdrive_shared_folder_id"
77+ GDRIVE_ID = "{{ task_instance.xcom_pull('get_shared_drive_id') }}"
78+
7479
7580log = logging .getLogger (__name__ )
7681
8085 start_date = datetime (2021 , 1 , 1 ),
8186 catchup = False ,
8287 tags = ["example" , "gcs" , "gdrive" ],
88+ render_template_as_native_obj = True ,
8389) as dag :
90+ @task
91+ def get_shared_drive_id () -> str :
92+ return get_secret (secret_id = GDRIVE_SECRET_ID ).strip ()
93+
94+
95+ get_shared_drive_id_task = get_shared_drive_id ()
8496
8597 @task
8698 def create_connection (connection_id : str ):
@@ -124,6 +136,7 @@ def create_connection(connection_id: str):
124136 source_bucket = BUCKET_NAME ,
125137 source_object = f"{ TMP_PATH } /{ FILE_NAME } " ,
126138 destination_object = f"{ WORK_DIR } /copied_{ FILE_NAME } " ,
139+ destination_folder_id = GDRIVE_ID ,
127140 )
128141 # [END howto_operator_gcs_to_gdrive_copy_single_file]
129142
@@ -134,7 +147,7 @@ def create_connection(connection_id: str):
134147 source_bucket = BUCKET_NAME ,
135148 source_object = f"{ TMP_PATH } /{ FILE_NAME } " ,
136149 destination_object = f"{ WORK_DIR } /copied_{ FILE_NAME } " ,
137- destination_folder_id = FOLDER_ID ,
150+ destination_folder_id = GDRIVE_ID ,
138151 )
139152 # [END howto_operator_gcs_to_gdrive_copy_single_file_into_folder]
140153
@@ -145,6 +158,7 @@ def create_connection(connection_id: str):
145158 source_bucket = BUCKET_NAME ,
146159 source_object = f"{ TMP_PATH } /*" ,
147160 destination_object = f"{ WORK_DIR } /" ,
161+ destination_folder_id = GDRIVE_ID ,
148162 )
149163 # [END howto_operator_gcs_to_gdrive_copy_files]
150164
@@ -155,23 +169,31 @@ def create_connection(connection_id: str):
155169 source_bucket = BUCKET_NAME ,
156170 source_object = f"{ TMP_PATH } /*.txt" ,
157171 destination_object = f"{ WORK_DIR } /" ,
172+ destination_folder_id = GDRIVE_ID ,
158173 move_object = True ,
159174 )
160175 # [END howto_operator_gcs_to_gdrive_move_files]
161176
162177 @task (trigger_rule = TriggerRule .ALL_DONE )
163- def remove_files_from_drive ():
178+ def remove_files_from_drive (** context ):
179+ ti = context ["ti" ]
164180 service = GoogleDriveHook (gcp_conn_id = CONNECTION_ID ).get_conn ()
165181 root_path = (
166182 service .files ()
167- .list (q = f"name = '{ WORK_DIR } ' and mimeType = 'application/vnd.google-apps.folder'" )
183+ .list (
184+ q = f"name = '{ WORK_DIR } ' and mimeType = 'application/vnd.google-apps.folder'" ,
185+ corpora = "drive" ,
186+ driveId = ti .xcom_pull ("get_shared_drive_id" ),
187+ includeItemsFromAllDrives = True ,
188+ supportsAllDrives = True ,
189+ )
168190 .execute ()
169191 )
170192 if files := root_path ["files" ]:
171193 batch = service .new_batch_http_request ()
172194 for file in files :
173- log .info ("Preparing to remove file: {} " , file )
174- batch .add (service .files ().delete (fileId = file ["id" ]))
195+ log .info ("Deleting file %s... " , file [ "name" ] )
196+ batch .add (service .files ().delete (fileId = file ["id" ], supportsAllDrives = True ))
175197 batch .execute ()
176198 log .info ("Selected files removed." )
177199
@@ -188,7 +210,7 @@ def delete_connection(connection_id: str) -> None:
188210 delete_connection_task = delete_connection (connection_id = CONNECTION_ID )
189211
190212 # TEST SETUP
191- create_bucket >> [upload_file_1 , upload_file_2 ]
213+ get_shared_drive_id_task >> create_bucket >> [upload_file_1 , upload_file_2 ]
192214 (
193215 [upload_file_1 , upload_file_2 , create_connection_task ]
194216 # TEST BODY
0 commit comments