2017-09-21 12 views
0

외부 클라이언트에서 ESXi 서버의 VM으로 파일을 보내려고합니다. VMware SDK VMWare.Vim.dll을 사용하고 있습니다. FileTransfer 요청에 지정된 URL을 사용하려고하면 실패합니다. "vmware_soap_session ="5e24eaa312s1245de2gg213456m23b4bd87c8e1ca "VMware SDK를 사용하여 VM 디렉토리에 파일 보내기 InitiateFileTransferToGuest 프로토콜 오류 -> ResponseStream 오류 22 <파일 이름이 유효하지 않습니다>

WebException이 : 원격 서버에서 오류를 반환했습니다 : (500) 내부 서버 오류 는"https://199.199.0.1:443/guestFile?id=22&token=23e2deef-7979-b1a8-75e5-413440d8c1377는 " 쿠키처럼 보이는 : 코드 : 같은

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, 
     DSSAuth, 
     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이 보이는

ResponseStream : \ n 22 \ n 파일 이름이 유효하지 않습니다.

누구나 VMware SDK로 파일 전송 경험이 있습니까? 도와 줘서 고마워!

답변

0

btw. 그것은 단순한 실수였습니다. 모든 슬래시는 이중 백 슬래시로 전환해야합니다. VM웨어 SDK는 슬래시를 처리 할 수 ​​있지만 웹 클라이언트는 처리 할 수 ​​없습니다.

예 :

string targetDir = BasePath + "\\" + Folder; 

string srcPath = BasePath + *"\\" + Folder + "\\" + FileName;