作業成功完成,且您也已從已完成的作業回應中擷取資源名稱,就可以使用 media.download
方法下載含有所產生 SDF 的 ZIP 檔案。
以下範例說明如何使用用戶端程式庫下載產生的檔案:
Java
// Extract download file resource name to use in download request String downloadResourceName = operationResponse.getResponse() .get("resourceName").toString(); // Configure the Media.download request Media.Download downloadRequest = service .media() .download(downloadResourceName); // Create output stream for downloaded file FileOutputStream outStream = new FileOutputStream(output-file); // Download file downloadRequest.executeMediaAndDownloadTo(outStream); System.out.printf("File downloaded to %s\n", outputFile);
Python
# Extract download file resource name to use in download request resourceName = operation["response"]["resourceName"] # Configure the Media.download request downloadRequest = service.media().download_media(resourceName=resourceName) # Create output stream for downloaded file outStream = io.FileIO(output-file, mode='wb') # Make downloader object downloader = googleHttp.MediaIoBaseDownload(outStream, downloadRequest) # Download media file in chunks until finished download_finished = False while download_finished is False: _, download_finished = downloader.next_chunk() print("File downloaded to %s" % output-file)
PHP
// Get client and set defer so it doesn't immediately return. $client = $this->service->getClient(); $client->setDefer(true); // Build media download request. $request = $this->service->media->download( $operation->getResponse()['resourceName'], array('alt' => 'media') ); // Call the API, getting the generated SDF. $response = $client->execute($request); $responseBody = $response->getBody(); // Writes the downloaded file. If the file already exists, it is // overwritten. file_put_contents(output-file, $responseBody); $client->setDefer(false); printf('File saved to: %s\n', output-file);
檔案下載和解壓縮後,即可使用產生的結構化資料檔案。產生的檔案會包含可識別 SDF 檔案類型的一般名稱 (例如:SDF-LineItems.csv
)。
略過的檔案
如果對應的結構化資料檔案不包含要求的資源資料,則下載的 ZIP 檔案可能會包含「已略過」的檔案 (例如:SDF-LineItems-Skipped.csv
)。這個檔案會有雙欄結構,第一份包含不可納入的資源 ID,第二個則包含排除原因的 ID。
系統可能會略過資源的原因有很多,包括處於不支援的狀態或類型不支援的類型。請使用最新的 SDF 版本,避免略過資源。