Upload BIG files via HTTP

后端 未结 3 1868
孤街浪徒
孤街浪徒 2020-12-08 23:42

I\'m trying to upload really big VM Images (5-15 Gb size) to an HTTP server using PowerShell.

I tried to use for that few methods (here links to script with net.WebC

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 00:33

    Thank you @Stoune, it was last thing that helped to receive finally working solution.

    One more, it is need to organize stream file reading and writing to the webrequest buffer. And it possibly to do with that piece of code:

    $requestStream = $webRequest.GetRequestStream()
    $fileStream = [System.IO.File]::OpenRead($file)
    $chunk = New-Object byte[] $bufSize
      while( $bytesRead = $fileStream.Read($chunk,0,$bufsize) )
      {
        $requestStream.write($chunk, 0, $bytesRead)
        $requestStream.Flush()
      }
    

    And final script look like this:

    $user = "admin"
    $pass = "admin123"
    $dir = "C:\Virtual Hard Disks"
    $fileName = "win2012r2std.vhdx"
    $file = "$dir/$fileName"
    $url = "http://nexus.lab.local:8081/nexus/content/sites/myproj/$fileName"
    $Timeout=10000000
    $bufSize=10000
    
    $cred = New-Object System.Net.NetworkCredential($user, $pass)
    
    $webRequest = [System.Net.HttpWebRequest]::Create($url)
    $webRequest.Timeout = $timeout
    $webRequest.Method = "POST"
    $webRequest.ContentType = "application/data"
    $webRequest.AllowWriteStreamBuffering=$false
    $webRequest.SendChunked=$true # needed by previous line
    $webRequest.Credentials = $cred
    
    $requestStream = $webRequest.GetRequestStream()
    $fileStream = [System.IO.File]::OpenRead($file)
    $chunk = New-Object byte[] $bufSize
      while( $bytesRead = $fileStream.Read($chunk,0,$bufsize) )
      {
        $requestStream.write($chunk, 0, $bytesRead)
        $requestStream.Flush()
      }
    
    $responceStream = $webRequest.getresponse()
    #$status = $webRequest.statuscode
    
    $FileStream.Close()
    $requestStream.Close()
    $responceStream.Close()
    
    $responceStream
    $responceStream.GetResponseHeader("Content-Length") 
    $responceStream.StatusCode
    #$status
    

提交回复
热议问题