[英]C# Sending File to VM Directory using VMware SDK InitiateFileTransferToGuest Protocol Error --> ResponseStream Error 22 <The File Name is not valid>
I am trying to send a file to a VM on a ESXi Server from an outside Client.我正在尝试从外部客户端向 ESXi 服务器上的虚拟机发送文件。 I am using the VMware SDK VMWare.Vim.dll.我正在使用 VMware SDK VMWare.Vim.dll。 When i try to use the URL given by the FileTransfer Request it Fails.当我尝试使用 FileTransfer 请求提供的 URL 时,它失败了。 Code:代码:
public static string ServerIP
{
get { return ConfigurationManager.AppSettings["ServerIP"].ToString(); }
}
private string ServiceUrl = "https://" + ServerIP + "/sdk";
private string UserName = "ESXIUser";
ServiceContent sc = new ServiceContent();
VimClient client = new VimClientImpl();
public VmOps()
{
}
public void Connect(string password)
{
// connect to vSphere web service
sc = client.Connect(ServiceUrl);
// Login using username/password credentials
client.Login(UserName, password);
}
public void copyDatastoreFile(/*string VMName, string Folder, string sourceFilePath*/)
{
string hostpass = "ESXIUserPAssword";
string VMName = "VMname";
string Folder = "Test";
string FileName = "test.zip";
//File destination info
string BasePath = "D:";
String targetDir = BasePath + "/" + Folder;
string srcPath = BasePath + "/" + Folder + "/" + FileName;
//Connect to the ESXi
Connect(hostpass);
NameValueCollection nvcFilter = new NameValueCollection();
nvcFilter.Add("Name", VMName);
var vm = (VirtualMachine)client.FindEntityView(typeof(VirtualMachine), null, nvcFilter, null);
GuestOperationsManager gom = new GuestOperationsManager(client, sc.GuestOperationsManager);
gom.UpdateViewData();
if (gom.FileManager == null)
{
return;
}
GuestFileManager gfm = new GuestFileManager(client, gom.FileManager);
var Auth = new NamePasswordAuthentication { Password = "VMPassword", Username = "VMUSER", InteractiveSession = false };
bool exists = false;
GuestListFileInfo fInfo = gfm.ListFilesInGuest(vm.MoRef, Auth, BasePath, null,null, Folder);
exists = fInfo.Files != null;
if (!exists)
{
// Create directory where the files will be copied to
gfm.MakeDirectoryInGuest(vm.MoRef, Auth, targetDir, true);
}
// Copy Virtual Machine file To
string URL = gfm.InitiateFileTransferToGuest(vm.MoRef,
Auth,
targetDir + "/" + FileName,
new GuestFileAttributes(),
new FileInfo(srcPath).Length,
true);
UploadFile(srcPath, URL.Replace("*", ServerIP));
}
private void UploadFile(string from, string to)
{
using (WebClient wc = new WebClient())
{
string cookie = ((VimApi_60.VimService)client.VimService).CookieContainer.GetCookies(new Uri(((VimApi_60.VimService)client.VimService).Url))[0].ToString();
wc.Credentials = new NetworkCredential("ESXiUSER", "ESXIUserPAssword");
wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
try
{
wc.UploadFile(to, "PUT", from);
}
catch (WebException wex)
{
if (wex.Response != null)
{
string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
}
}
wc.Dispose();
}
}
URL Looks like: " https://199.199.0.1:443/guestFile?id=22&token=23e2deef-7979-b1a8-75e5-413440d8c1377 " Cookie Looks like: "vmware_soap_session="5e24eaa312s1245de2gg213456m23b4bd87c8e1ca" URL 看起来像:“ https://199.199.0.1:443/guestFile?id=22&token=23e2deef-7979-b1a8-75e5-413440d8c1377 ” Cookie 看起来像:“vmware_soap_session="5e24ea21534b1g28c8c8c8c8c8c8c8c75e5c
WebException: The remote Server returned an error:(500) Internal Server Error WebException:远程服务器返回错误:(500) 内部服务器错误
ResponseStream: \\n 22\\n The file Name is not valid ResponseStream: \\n 22\\n 文件名无效
Anyone any experience on file Transfers with VMware SDK?任何人有使用 VMware SDK 进行文件传输的经验吗? Thank you for your help!感谢您的帮助!
btw.顺便说一句。 it was a simple mistake, all slashs have to be switched with a double backslash.这是一个简单的错误,所有斜杠都必须用双反斜杠切换。 The VMWare sdk can handle the slash, but the webclient put can't. VMWare sdk 可以处理斜杠,但 webclient put 不能。
example:例子:
string targetDir = BasePath + "\\" + Folder;
string srcPath = BasePath + *"\\" + Folder + "\\" + FileName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.