WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud Foundation
  • VKS
  • Homelab
    • Resources
    • Nested Virtualization
  • VMware Nostalgia
  • Apple
You are here: Home / ESXi / How to efficiently transfer files to Datastore in vCenter using the vSphere API?

How to efficiently transfer files to Datastore in vCenter using the vSphere API?

06.18.2014 by William Lam // 19 Comments

A pretty common task for vSphere Administrators is to upload or download content from a vSphere Datastore which usually contains ISOs and floppy images. You can initiate the file transfer using the vSphere Web/C# Client, however this process can be quite tedious when having to manually upload several ISOs. Instead, you will probably want to automate this process and and there are a couple of ways in which you can accomplish this. One option, is to go directly to an ESXi host and upload your files but this is not ideal when you have vCenter Server to centrally manage your infrastructure. The second option is to go through vCenter Server, but depending on the implementation, you can potentially add unnecessary load to the vCenter Server if implemented incorrectly.

Let me explain this further with two diagrams and you can decide which implementation you prefer?
inefficent-file-transfer-to-datastore
In this first implementation, I directly access the file management API which leverages a simple HTTP GET/PUT operation to upload files to a vSphere Datastore. What I found out while transferring the data was that the data actually traverses through the vCenter Server and then onto the ESXi host before writing to the vSphere Datastore. This of course made the data transfer very inefficient not to mention additional bandwidth and load being added to vCenter Server.

I created a sample vSphere SDK for Perl script that demonstrates this inefficent transfer called inefficent-upload-files-to-datastore.pl

Here is sample execution of the script which accepts the name of the vSphere Datacenter, vSphere Datastore, the source file to transfer and the destination path of where the file will be uploaded to:

./inefficent-upload.pl --config ~/vmware-dev/.vcenter55-1 --datacenter Datacenter --datastore vsanDatastore --sourcefile /Volumes/Storage/Images/CentOS-6.4-x86_64-netinstall.iso --destfile ISO/CentOS-6.4-x86_64-netinstall.iso

After talking to some folks about this problem, I learned about a more efficient method as shown in the diagram below.
efficent-file-transfer-to-datastore.png
As you can see, we can still initiate the transfer using the vCenter Server, but the actual data transfer is than sent to one of the ESXi hosts that has access to the vSphere Datastore. To accomplish this, we need to use the AcquireGenericServiceTicket() method which is part of the sessionManager. Using this method, we can request a ticket for a one time HTTP request to connect directly to an ESXi. To upload a file, the request must include the method which in this case will be a PUT operation and the local URL to an ESXi host that has access to the vSphere Datastore.

Here is an example of a URL: https://vesxi55-1.primp-industries.com/folder/ISO/CentOS-6.4-x86_64-netinstall.iso?dcPath=ha-datacenter&dsName=vsanDatastore

  • ESXi IP Address/Hostname - In the script, I select the first ESXi host that has access to the vSphere Datastore
  • vSphere Datastore Directory - Directory into which the contents of the file will be placed in. In this example, we just have one top-level directory called ISO which must already exist
  • Destination file name - The name of the file that should appear in the vSphere Datastore
  • Datacenter Name - This should always be ha-datacenter when connecting directly to an ESXi host
  • vSphere Datastore - The name of the vSphere Datastore

To demonstrate this functionality, I have created a vSphere SDK for Perl script called efficent-upload-files-to-datastore.pl which accepts the name of the vSPhere Datastore along with the source and destination of where the file will be placed:

./upload-files-to-datastore.pl --config ~/vmware-dev/.vcenter55-1 --datastore vsanDatastore --sourcefile /Volumes/Storage/Images/CentOS-6.4-x86_64-netinstall.iso --destfile ISO/CentOS-6.4-x86_64-netinstall.iso

Hopefully after looking at these two implementations, you will also agree that the second option is the best! One last thing that I would like to point out is that even though we are talking about transferring files to a vSphere Datastore, this method can also be used to efficiently transfer other supported files to an ESXi hosts through vCenter Server as described in this blog article.

More from my site

  • PowerCLI script to help correlate vCenter, ESXi & vSAN build/versions w/o manual VMware KB lookup
  • How to move vSAN Datastore into a Folder?
  • How to build custom ESXi ISO for Apple Mac Mini?
  • Quick Tip - Offline viewing of vSphere API & other API docs using Dash
  • Quick Tip - Correlating VMFS Datastore to Storage Device Using ESXCLI

Categories // ESXi, vSphere Tags // datastore, HTTP, iso, vSphere, vSphere API

Comments

  1. *protectedJim Millard says

    06/19/2014 at 12:01 am

    This seems like a great time to point out a common practice I use in client environments: a filer volume that offers simultaneous NFS & SMB access. That allows the admin to completely bypass the vSphere environment by using his/her workstation to manage the file store using native tools while the connected ESXi hosts have access for use by guests. An additional admin perk is that the datastore name (eg, "ISOs") makes it readily apparent when a guest is using a file on the datastore.

    Reply
    • William Lam says

      06/20/2014 at 1:29 pm

      Though the tip you provide is an excellent one, in my example I was using VSAN datastore so this would not be possible 🙂

      Reply
  2. *protectedAnton says

    06/19/2014 at 2:13 pm

    I use FileZilla, connect directly to host (ssh enabled) and upload everything very fast.
    Good idea with NFS and SMB, but it'll not help if you have VMFS.

    Reply
    • William Lam says

      06/20/2014 at 1:31 pm

      Definitely another option. Though the solution listed above does not require SSH which not all customers have enabled and definitely would not work if you're using VMFS or VSAN.

      Reply
  3. *protectedVarun says

    07/17/2014 at 9:37 pm

    Thanks for the information. This is exactly the problem that i was trying to solve.
    But i wanted some help regarding the use of the AcquireGenericServiceTicket() method. While i was able to download the file for the ESX host version 5.5 but it failed for the versions 5.1 and 4.x. Is this method specific to any host version or we need to enable anything on the esx host for this to work.

    Reply
    • William Lam says

      07/18/2014 at 3:25 am

      In the vSphere API documentation, the API has been available sine 5.0, so it would not work with 4.x

      Reply
  4. *protectedVarun says

    07/18/2014 at 5:09 pm

    Thanks for the reply.
    So it should work on all the 5.x hosts. But it seems that this api is not working on the 5.1 hosts. It always fails when trying to acquire the ticket.
    Below is the error that i get from the logs.:
    "A specified parameter was not correct. url"
    I have verified that the url i am using is correct. Are we required to configure any settings on the ESX host for this to work?

    Reply
    • William Lam says

      07/18/2014 at 5:13 pm

      No configurations are required on the ESXi host, as long as the VC can communicate to ESXi host through port 443, that is all that is needed. I don't have a 5.1 infrastructure to test on. If you have SNS, you can file an SR to see if GSS can see if there is a known issue w/5.1

      Reply
    • *protectedSean Dilda says

      08/13/2014 at 12:08 am

      Have you had any luck getting this working with 5.1? I have access to a 5.1 and 5.5 setup. With 5.5, I was able to get this working well (after granting my user the Host->Configuration->System Management privilege). However, when I try against my 5.1 setup, I get the same error, even after fixing the permissions.

      Reply
  5. *protectedSean Dilda says

    08/12/2014 at 8:11 pm

    Thanks for posting this. This is going to help me get around a nasty situation I just ran into... vCenter isn't ignoring disconnected hosts when selecting a host to transfer through, but the disconnected state causes the file upload to fail. Which also leads to a possible improvement to your efficient script.

    The mount status for a host doesn't change when a host is disconnected (and likely when its not responding/down). It'd probably be worth verifying the host's runtime.connectionState is good when selecting a host.

    Reply
  6. *protectedsteve88 says

    06/14/2015 at 3:14 pm

    I noticed Filezilla worked perfects. I had issues with WinSCCP also where in FileZilla it takes 5 minutes and in the VCenter GUI and WinSCP it was still increasing higher above 2 hours. Simply put FileZilla was a star

    Reply
  7. *protectedKERR says

    09/14/2015 at 6:27 am

    Yep Filezilla over SCP is a LOT faster.

    Reply
  8. *protectedmrhhug says

    11/29/2015 at 3:23 pm

    Looks like you are using an octet-stream http put - currently doing the same here. I am intermitantly having problems putting large files on some of my testing environments. It is probobly due to wacky network trafficin these environemnts. When I ask the http server for OPTIONS, the http server chooses not to list the options. Do the http puts accept encoding or chunking? Chunking would be sweet.

    Reply
  9. *protectedUlrar says

    02/09/2016 at 6:08 am

    Hi,

    That seems useful, but I see no explanation on how to create that config file ? (for --config)
    I'm guessing it has to do with setting up the API, which seems hard enough to install on non-rpm systems, but I can't find an easy example.

    Reply
  10. *protectedManish says

    11/10/2016 at 4:31 am

    Hi, i have a very low bandwidth, 1mb and takes 10hrs to copy iso to datastore on remote esxi.
    what is the efficient way to transfer data to remote esxi. USb ? or ..

    Reply
  11. *protectedDavid Chopin says

    03/03/2017 at 4:50 am

    Very interesting article, thanks William ! Is there a difference on the way vCenter access these datastores comparing vSphere 5.5 C# client and vSphere 6.5 Web Client ? On one of my lab with 2 vCenters being in country A and the ESXi host being in country B :

    - When connecting to the vCenter 5.5 through the vSphere C# client from a VM hosted on my ESXi host in country B, transferring a file from the said VM to a local datastore seems to bypass vCenter Control + Data and go directly from ESXi host to the datastore. The transfer is then very fast as being local I guess (between the ESXi host and the storage array)

    - When connecting to the vCenter 6.5 through the vSphere Web client from a VM hosted on my ESXi host in country B, transferring a file from the said VM to a local datastore seems to go through the vCenter, then come back to the ESXi host, then to the datastore. The transfer is then very slow, going back and forth between the vCenter in country A and the ESXi host being in country B.

    Is this a normal behaviour ? If so, is there a way, in vSphere 6.5 Web Client to avoid going back and forth between vCenter and ESXi host ? Or should I use the provided script and/or WinSCP for example ?

    Thanks !

    Reply
  12. *protectedBernhard says

    02/05/2020 at 11:46 am

    I need to upload ISO files from a python3 script to a vmware datastore.
    I know how to deal with HTTP connections but how do I translate Vim:: calls?
    BTW: it would be very cool if I could use REST calls.

    Reply
    • *protectedBernhard says

      02/09/2020 at 11:29 am

      I found a solution, https://vc/folder is the key. Now I just have to find out if I can use the auth-token from my REST connection. At the moment I use 2 separate connections.

      Reply
  13. *protectedHU says

    03/13/2024 at 1:41 pm

    Hello, a little late to the party here.
    How about moving a file to a content library?

    Reply

Leave a Reply to mrhhugCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search

Thank Author

Author

William is Distinguished Platform Engineering Architect in the VMware Cloud Foundation (VCF) Division at Broadcom. His primary focus is helping customers and partners build, run and operate a modern Private Cloud using the VMware Cloud Foundation (VCF) platform.

Connect

  • Bluesky
  • Email
  • GitHub
  • LinkedIn
  • Mastodon
  • Reddit
  • RSS
  • Twitter
  • Vimeo

Recent

  • Programmatically accessing the Broadcom Compatibility Guide (BCG) 05/06/2025
  • Quick Tip - Validating Broadcom Download Token  05/01/2025
  • Supported chipsets for the USB Network Native Driver for ESXi Fling 04/23/2025
  • vCenter Identity Federation with Authelia 04/16/2025
  • vCenter Server Identity Federation with Kanidm 04/10/2025

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2025

 

Loading Comments...