下載檔案

結構化資料檔案成功產生後,請使用 media.download 從提供的資源位置下載產生的 ZIP 檔案。

如要擷取檔案位置並下載 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}.')

解壓縮檔案

解壓縮下載的 ZIP 檔案,擷取產生的結構化資料檔案。這些檔案的名稱會識別檔案類型,例如 SDF-LineItems.csv

如果請求的資源的資料無法包含在產生的結構化資料檔案中,則 ZIP 文件也會包含該資源的「已跳過」文件,例如 SDF-LineItems-Skipped.csv。「已略過」檔案的結構為兩欄。第一列標識被跳過的資源,第二列包含解釋其被排除的錯誤訊息。