操作成功完成并且您从已完成的操作响应中提取资源名称后,您就可以使用 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,第二列包含排除的原因。
可能因多种原因而跳过资源,包括处于不受支持的状态或类型不受支持。避免使用最新版本的 SDF 跳过资源。