This example shows how to copy library item files to the client system by using an HTTP transfer.
This example uses the steps that are described in the Download Files to a Local System from a Library Item procedure.
Note: For related code samples, see the
vSphere Automation SDK .NET samples at GitHub.
... // Access the DownloadSession service. var downloadSessionService = ServiceManager.VapiConnection.GetService<DownloadSession>(); // Create a new download session model. var downloadSessionModel = new DownloadSessionModel(); downloadSessionModel.SetLibraryItemId(libItem.GetId()); var downloadSessionId = downloadSessionService.Create(Guid.NewGuid().ToString(),downloadSessionModel); var httpClient = new HttpClient(true); // Access the File service and retrieve the files you want to export. var downloadFileService = ServiceManager.VapiConnection.GetService<vmware.content.library.item.downloadsession.File>(); for (var downloadFileInfo in downloadFileService.List(downloadSessionId)) { // Make sure all files are in prepared state before you precede with the downloading operation downloadFileService.Prepare(downloadSessionId, downloadFileInfo.GetName(), vmware.content.library.item.downloadsession.FileTypes.EndpointType.HTTPS); long timeOut = 360; long endTime = DateTime.Now.Millisecond + timeOut * 1000; Thread.sleep(5000); var expectedStatus =vmware.content.library.item.downloadsession.File.PrepareStatus.PREPARED; downloadFileInfo = downloadFileService.Get(downloadSessionId,downloadFileInfo.GetName()); var 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()); var downloadUrl = downloadFileInfo.GetDownloadEndpoint().GetUri().ToString(); // Execute an HTTP GET request and pass the URLs to the endpoints of the download files. using(var inputStream = httpClient.DownloadFile(downloadUrl)) { var tmpDirInfo = Directory.CreateDirectory("tmp"); var fullLocalPath = Path.Combine(tmpDirInfo.FullName, downloadFileInfo.GetName()); using(var localFile = File.Create(fullLocalPath)) { // Copy the files to the directory on your machine inputStream.CopyTo(localFile); } } } else { while (endTime > DateTime.Now.Millisecond) { downloadFileInfo = downloadFileService.Get(downloadSessionId, downloadFileInfo.GetName()); currentStatus = downloadFileInfo.GetStatus(); if (currentStatus == expectedStatus) { return; } else if (currentStatus == vmware.content.library.item.downloadsession.File.PrepareStatus.ERROR) { Console.WriteLine("DownloadSession Info : " + downloadSessionService.Get(downloadSessionId)); throw new Exception("Error while waiting for download file status to be PREPARED..."); } } } }