Perl 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.pm 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 sample code, see the vSphere Automation SDK Perl samples at GitHub.
...

# 1 - Create a new library item to hold the uploaded file.
my $item_model = new
   Com::Vmware::Content::Library::ItemModel();
$item_model->set_name('name' => 'ESXi patches');
$item_model->set_description('description' => 'ESXi security patches');
$item_model->set_type('type' => 'iso');
$item_model->set_library_id('library_id' => $my_library_id);
my $idem_token = ContentLibrary::Helpers::ClsApiHelper::generate_uuid();
my $item_stub = $stubFactory->create_stub(
   'service_name' => 'Com::Vmware::Content::Library::Item',
   'stub_config'  => $myStubConfig );
my $my_library_item_id = $item_stub->create('create_spec'  => $item_model,
                                            'client_token' => $idem_token);

# 2 - Create an UpdateSessionModel instance to track your changes to the item.
my $update_session_model = new
   Com::Vmware::Content::Library::Item::UpdateSessionModel();
$update_session_model->set_library_item_id('library_item_id' => $my_library_item_id);

# 3 - Create an update session from the model.
$idem_token = ContentLibrary::Helpers::ClsApiHelper::generate_uuid();
my $update_session_stub = $stubFactory->create_stub(
   'service_name' => 'Com::Vmware::Content::Library::Item::UpdateSession',
   'stub_config'  => $myStubConfig );
my $update_session_id =
   $update_session_stub->create('create_spec'  => $update_session_model,
                                'client_token' => $idem_token);

# 4 - Create a new AddSpec instance to describe the properties of
#     the file to be uploaded.
my $file_spec = new
   Com::Vmware::Content::Library::Item::Updatesession::File::AddSpec();
$file_spec->set_name('name' => 'ESXi patch');
$file_spec->set_source_type('type' =>
 Com::Vmware::Content::Library::Item::Updatesession::File::SourceType::PULL);

# 5 - Specify the location from which the file is to be uploaded.
my $endpoint = new
   Com::Vmware::Content::Library::Item::TransferEndpoint();
$endpoint->set_uri('uri' => 'http://www.example.com/patches_ESXi65/ESXi_patch.iso');
$file_spec->set_source_endpoint('source_endpoint' => $endpoint);

# 6 - Link the file specification to the update session.
my $update_file_stub = $stubFactory->create_stub(
   'service_name' => 'Com::Vmware::Content::Library::Item::Updatesession::File',
   'stub_config'  => $myStubConfig );
$update_file_stub->add('update_session_id' => $update_session_id,
                       'file_spec'         => $file_spec);

# 7 - Mark session as completed, to initiate the asynchronous transfer.
$update_session_stub->complete('update_session_id' => $update_session_id);