This example shows how to upload a file from a URL to a library item. The example is based on the code in the content_library_workflow.py sample file. The sample resource is located in the vCloud Suite SDK for Python directory, client/samples/src/com/vmware/vcloud/suite/sample/workflow.

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

from com.vmware.content import library_client
from com.vmware.content.library import item_client
from com.vmware.content.library.item import update_session_client
import uuid
 
# 1 - Create a new library item to hold the uploaded file.
item_model = library_client.ItemModel()
item_model.name = ’ESXi patches’
item_model.description = ’ESXi 5.5 security patches as of 2/3/2015’
item_model.type = ’iso’
item_model.library_id = my_library_id
idem_token = str(uuid.uuid4())
item_stub = library_client.Item(my_stub_config)
item_id = item_stub.create(create_spec=item_model, client_token=idem_token)
 
# 2 - Create an UpdateSessionModel instance to track the changes you make to the item.
update_session_model = item_client.UpdateSessionModel()
update_session_model.library_item_id = item_id
 
# 3 - Create an update session from the model.
idem_token = str(uuid.uuid4())
update_session_stub = update_session_client.UpdateSession(my_stub_config)
session_id = update_session_stub.create(create_spec=update_session_model, client_token=idem_token)
 
try :
  # 4 - Create a new AddSpec instance to describe the properties of the file to be uploaded.
  file_spec = update_session_client.AddSpec()
  file_spec.name = ’ESXi patch as of 10/2/2015’
  file_spec.source_type = update_session_client.File.SourceType.PULL
 
  # 5 - Specify the location from which the file is to be uploadod to the library item.
  endpoint = item_client.TransferEndpoint()
  endpoint.uri = ’http://www.example.com/patches_ESXi55/ESXi_patch.iso’
  file_spec.source_endpoint = endpoint
 
  # 6 - Link the file specification to the update session.
  update_file_stub = update_session_client.File(my_stub_config)
  update_file_stub.File.add(update_session_id=session_id, file_spec=file_spec)
 
  # 7 - Mark session as completed, to initiate the asynchronous transfer.
  update_session_stub.complete(session_id)