Download Files to a
Local System from a Library Item
You might want to
download files to a local system from a library item and then make changes to
the files before you use them.
- Create a download session model to specify the item, which contains the file that you want to download.
- Access theFileservice and retrieve the file that you want to export to your system within the new download session.
- Prepare the files that you want to download and wait until the files are in the prepared state.
- Retrieve the download endpoint URI of the files.
- Download the files with an HTTP GET request.
- Delete the download session after all files are downloaded.
The files are downloaded from the library item to the local
system.
- Java
- This example is based on the code in theItemDownloadHelper.javasample file.This example uses the steps that are described in the Download Files to a Local System from a Library Item procedure.For a complete and up-to-date version of the Java sample code, see thevsphere-automation-sdk-javaVMware repository at GitHub.... // Access the DownloadSession service. DownloadSession downloadSessionService = vapiAuthHelper.getStubFactory().createStub(DownloadSession.class, sessionStubConfig); // Create a new download session model. DownloadSessionModel downloadSessionModel = new DownloadSessionModel(); downloadSessionModel.setLibraryItemId(libItem.getId()); String downloadSessionId = downloadSessionService.create(UUID.randomUUID().toString(), downloadSessionModel); // Access the File service and retrieve the files you want to export. File downloadFileService = vapiAuthHelper.getStubFactory().createStub(File.class, sessionStubConfig); List<FileTypes.Info> downloadFileInfos = downloadFileService.list(downloadSessionId); for (FileTypes.Info downloadFileInfo : downloadFileInfos) { // Make sure all files are in the prepared state before you precede with the downloading operation. downloadFileService.prepare(downloadSessionId, downloadFileInfo.getName(), EndpointType.HTTPS); long timeOut = 360; Long endTime = System.currentTimeMillis() + timeOut * 1000; try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println(e); } FileTypes.PrepareStatus expectedStatus = com.vmware.content.library.item.downloadsession.File.PrepareStatus.PREPARED; downloadFileInfo = downloadFileService.get(downloadSessionId, downloadFileInfo.getName()); FileTypes.PrepareStatus currentStatus = downloadFileInfo.getStatus(); if (currentStatus == expectedStatus) { // When the files are prepared, you can retrieve the download information for each file. downloadFileInfo = downloadFileService.get(downloadSessionId, downloadFileInfo.getName()); try { URI downloadUri = downloadFileInfo.getDownloadEndpoint().getUri(); String downloadUrl = downloadUri.toURL().toString(); // Run an HTTP GET request and pass the download endpoints of the files. HttpClient httpClient = new HttpClient(true); InputStream inputStream = httpClient.downloadFile(downloadUrl); String fileNameDownload = downloadFileInfo.getName(); File tmpDir = new java.io.File("tmp"); tmpDir.mkdir(); String fullPath = tmpDir.getAbsolutePath() + System.getProperty("file.separator") + fileNameDownload; // Copy the files to the directory on your machine. Files.copy(inputStream, Paths.get(fullPath),StandardCopyOption.REPLACE_EXISTING); } catch (MalformedURLException e) { System.out.println("Failed to download due to IOException!" + e); throw new RuntimeException("Failed to download due to IOException!", e); } catch (IOException e) { System.out.println("IO exception during download" + e); throw new RuntimeException("Failed to download due to IOException!", e); // Delete the download session after all files are downloaded. } finally { downloadFileService.delete(downloadSessionId); } } else { while (endTime > System.currentTimeMillis()) { downloadFileInfo = downloadFileService.get(downloadSessionId, downloadFileInfo.getName()); currentStatus = downloadFileInfo.getStatus(); if (currentStatus == expectedStatus) { return; } else if (currentStatus == com.vmware.content.library.item.downloadsession.File.PrepareStatus.ERROR) { System.out.println("DownloadSession Info : " + downloadSessionService.get(downloadSessionId)); throw new RuntimeException("Error while waiting for download file status to be PREPARED..."); } } } }
- Python
- This example uses the code in thecls_api_helper.pysample file.This example uses the steps that are described in the Download Files to a Local System from a Library Item procedure.For a complete and up-to-date version of the sample code, see thevsphere-automation-sdk-pythonVMware repository at GitHub.... # 1 - Create a new download session model. download_session_model = item_client.DownloadSessionModel() download_session_model.library_item_id = my_library_item_id download_session_stub = item_client.DownloadSession(my_stub_config) idem_token = str(uuid.uuid4()) download_session_id = download_session_stub.create(create_spec=download_session_model, client_token=idem_token) # 2 - Access the File service and retrieve the files you want to export. download_session_file_stub = download_session_client.File(my_stub_config) file_infos = download_session_file_stub.list(download_session_id) for file_info in file_infos : download_session_file_stub.prepare(download_session_id, file_info.name) # 3 - Wait until the file is in the prepared state before downloading. download_info = download_session_file_stub.get(download_session_id, file_info.name) while (DownloadSessionFile.PrepareStatus.PREPARED != download_info.status) : time.sleep(30) # 4 - Download the file with an HTTP GET request. response = urllib2.urlopen(download_info.download_endpoint.uri) file_path = os.path.join(my_directory, file_info.name) with open(file_path, ’wb’) as f : f.write(response.read()) # 5 - Delete the download session after all files are downloaded. download_session_stub.delete(download_session_id)