Java Example of Uploading a File from a URL to a Library Item

This example shows how to upload a file from a URL to a library item. The example is based on the code in the ItemUploadHelper.java sample file.

This example uses the steps that are described in the Upload a File from a URL to a Library Item procedure.

Note: For a complete and up-to-date version of the Java sample code, see the vsphere-automation-sdk-java VMware repository at GitHub.
...
 
// Create a new library item. See Create an Empty Library Item. 
 
...
 
// Access the com.vmware.content.library.item.updatesession.File 
// and the UpdateSession services by using the vSphere Automation Endpoint.
   File uploadFileService = this.vapiAuthHelper.getStubFactory().createStub(File.class, sessionStubConfig);
   UpdateSession uploadService = this.vapiAuthHelper.getStubFactory().createStub(UpdateSession.class, sessionStubConfig);

// Create an UpdateSessionModel instance to track the changes you make to the item.
        UpdateSessionModel updateSessionModel = new UpdateSessionModel();
        updateSessionModel.setLibraryItemId(newItem);

// Create a new update session. 
        String clientToken = UUID.randomUUID().toString();
        String sessionId = uploadService.create(clientToken, updateSessionModel);

// Create a new AddSpec instance to describe the properties of the file to be uploaded.
         FileTypes.AddSpec fileSpec = new AddSpec();
         fileSpec.setName("ESXi patch");
         fileSpec.setSourceType(SourceType.PULL);

// Specify the location from which the file is uploaded to the library item.
         TransferEndpoint endpoint = new TransferEndpoint();
         endpoint.setUri(URI.create("http://www.acme.com/patches_ESXi65/ESXi_patch.iso"));
         fileSpec.setSourceEndpoint(endpoint);
         uploadFileService.add(sessionId, fileSpec);

// Mark the session as completed. 
         uploadService.complete(sessionId);