Tải tệp xuống

Sau khi tệp dữ liệu có cấu trúc tạo thành công, hãy tải tệp ZIP kết quả xuống từ vị trí tài nguyên được cung cấp bằng cách sử dụng media.download.

Sau đây là cách truy xuất vị trí của tệp và tải tệp ZIP xuống:

# 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}.')

Trích xuất tệp

Giải nén tệp ZIP đã tải xuống để trích xuất các tệp dữ liệu có cấu trúc đã tạo. Chúng có tên chung để xác định loại tệp, chẳng hạn như SDF-LineItems.csv.

Nếu không thể đưa dữ liệu cho một tài nguyên được yêu cầu vào tệp Dữ liệu có cấu trúc đã tạo, thì tệp ZIP cũng sẽ bao gồm một tệp "Đã bỏ qua" cho tài nguyên đó, chẳng hạn như SDF-LineItems-Skipped.csv. Các tệp "Bị bỏ qua" có cấu trúc gồm 2 cột. Cột đầu tiên xác định tài nguyên bị bỏ qua và cột thứ hai chứa thông báo lỗi giải thích lý do loại trừ.