Setelah File Data Terstruktur selesai dibuat dengan berhasil, download file ZIP yang dihasilkan dari lokasi resource yang diberikan menggunakan media.download.
Berikut cara mengambil lokasi file dan mendownload file ZIP:
# Import the object used for media download. from googleapiclient import http as googleHttp # Provide the name of the successfully completed sdfdownloadtask operation. operation_name = operation-name # Provide the path for the output file. output_path = output-path # Retrieve the completed operation. operation = service.sdfdownloadtasks().operations().get(operation_name ).execute() # Retrieve the file location from the operation. file_location = operation["response"]["resourceName"] # Configure the media.download request. download_request = service.media().download_media(resourceName=file_location) # Create output stream for downloaded file. out_stream = io.FileIO(output_path, mode='wb') # Build downloader object. downloader = googleHttp.MediaIoBaseDownload(out_stream, download_request) # Download file in chunks until finished. download_finished = False while download_finished is False: _, download_finished = downloader.next_chunk() print(f'File downloaded at {output_path}.')
Mengekstrak file
Dekompresi file ZIP yang didownload untuk mengekstrak File Data Terstruktur yang dihasilkan. File tersebut memiliki nama generik yang mengidentifikasi jenis filenya, seperti
SDF-LineItems.csv.
Jika data untuk resource yang diminta tidak dapat disertakan dalam file Data Terstruktur yang dibuat, file ZIP juga akan menyertakan file "Dilewati" untuk resource tersebut, seperti SDF-LineItems-Skipped.csv. File yang "Dilewati" memiliki struktur
dua kolom. Kolom pertama mengidentifikasi resource yang dilewati dan kolom kedua berisi pesan error yang menjelaskan pengecualiannya.