Python Example of Downloading Files from a Library Item to Your Local System

This example uses the code in the
cls_api_helper.py
sample 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 the vSphere Automation SDK Python samples 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)