Ruby 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.
This example uses the steps that are
described in the
Upload a File from a URL to a Library Item
procedure.
For related code
samples, see the
vSphere Automation SDK Ruby
samples at GitHub.
... # 1 - Create a new library item to hold the uploaded file. item_model = Com::Vmware::Content::Library::ItemModel.new item_model.name = 'ESXi patches' item_model.description = 'ESXi security patches' item_model.type = 'iso' item_model.library_id = $my_library_id idem_token = SecureRandom.uuid item_stub = Com::Vmware::Content::Library::ItemService.new $my_stub_config $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. update_session_model = Com::Vmware::Content::Library::Item::UpdateSessionModel.new update_session_model.library_item_id = $my_library_item_id # 3 - Create an update session from the model. idem_token = SecureRandom.uuid update_session_stub = Com::Vmware::Content::Library::Item::UpdateSession.new $my_stub_config 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. file_spec = Com::Vmware::Content::Library::Item::Updatesession::File::AddSpec.new file_spec.name = 'ESXi patch' file_spec.source_type = Com::Vmware::Content::Library::Item::Updatesession::File::SourceType::PULL # 5 - Specify the location from which the file is to be uploaded. endpoint = Com::Vmware::Content::Library::Item::TransferEndpoint.new endpoint.uri = 'http://www.example.com/patches_ESXi65/ESXi_patch.iso' file_spec.source_endpoint = endpoint # 6 - Link the file specification to the update session. update_file_stub = Com::Vmware::Content::Library::Item::Updatesession::File.new( $my_stub_config ) 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