2

how can i upload files larger than 2 gb to my FTP server using powershell, i am using the below function

# Create FTP Rquest Object

$FTPRequest = [System.Net.FtpWebRequest]::Create("$RemoteFile")
    $FTPRequest = [System.Net.FtpWebRequest]$FTPRequest
    $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $FTPRequest.Credentials = new-object System.Net.NetworkCredential($Username, $Password)
    $FTPRequest.UseBinary = $true
    $FTPRequest.Timeout = -1
    $FTPRequest.KeepAlive = $false
    $FTPRequest.ReadWriteTimeout = -1
    $FTPRequest.UsePassive = $true

    # Read the File for Upload

    $FileContent = [System.IO.File]::ReadAllBytes(“$LocalFile”)
    $FTPRequest.ContentLength = $FileContent.Length

# Get Stream Request by bytes

try{
    $Run = $FTPRequest.GetRequestStream()
    $Run.Write($FileContent, 0, $FileContent.Length)

# Cleanup

    $Run.Close()
    $Run.Dispose()    
} catch [System.Exception]{
    'Upload failed.'       
}

i am getting this error while uploading.

 Exception calling "ReadAllBytes" with "1" argument(s): "The file is too long. 
    This operation is currently limited to supporting files less than 2 gigabytes 
    in size."

    +     $FileContent = [System.IO.File]::ReadAllBytes(“$LocalFile”)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IOException

i have used other functions but the result was so slow upload speed like it was not going higher than 50KB/s hope there is a solution for this, other than splitting the large files into chunks

Vagho
  • 117
  • 1
  • 15
  • 2
    Possible duplicate of [Powershell read file in chunks](http://stackoverflow.com/questions/39345335/powershell-read-file-in-chunks) – sodawillow Jan 15 '17 at 09:55
  • i will check that out now .. – Vagho Jan 15 '17 at 09:57
  • 1
    `$FileStream = [System.IO.File]::OpenRead(“$LocalFile”); $FTPRequest.ContentLength = $FileStream.Length; $Run = $FTPRequest.GetRequestStream(); $FileStream.CopyTo($Run); $Run.Close(); $FileStream.Close();` – user4003407 Jan 15 '17 at 10:01
  • do you mean like this ? $FileStream = [System.IO.File]::OpenRead(“$LocalFile”); $FTPRequest.ContentLength = $FileStream.Length; $Run = $FTPRequest.GetRequestStream(); $FileStream.CopyTo($Run); $Run.Write($FileStream, 0, $FileStream.Length); $Run.Close() $FileStream.Close(); – Vagho Jan 15 '17 at 10:08
  • @Vagho Yes, like this, but without `$Run.Write($FileStream, 0, $FileStream.Length);`. – user4003407 Jan 15 '17 at 10:17
  • i was able to make it work using this too .. which one is better in your opinion ? $bufsize = 256mb $requestStream = $FTPRequest.GetRequestStream() $fileStream = [System.IO.File]::OpenRead($LocalFile) $chunk = New-Object byte[] $bufSize while ( $bytesRead = $fileStream.Read($chunk, 0, $bufsize) ){ $requestStream.write($chunk, 0, $bytesRead) $requestStream.Flush() } $FileStream.Close() $requestStream.Close() – Vagho Jan 15 '17 at 10:21

1 Answers1

1

thanks to PetSerAi and sodawillow

i have found 2 solutions that worked for me.

Solution 1 by : sodawillow

    $bufsize = 256mb
    $requestStream = $FTPRequest.GetRequestStream()
    $fileStream = [System.IO.File]::OpenRead($LocalFile)
    $chunk = New-Object byte[] $bufSize

    while ( $bytesRead = $fileStream.Read($chunk, 0, $bufsize) ){
        $requestStream.write($chunk, 0, $bytesRead)
        $requestStream.Flush()
    }

    $FileStream.Close()
    $requestStream.Close()

Solution 2 by : PetSerAi

    $FileStream = [System.IO.File]::OpenRead("$LocalFile")
    $FTPRequest.ContentLength = $FileStream.Length
    $Run = $FTPRequest.GetRequestStream()
    $FileStream.CopyTo($Run, 256mb)
    $Run.Close()
    $FileStream.Close()
Community
  • 1
  • 1
Vagho
  • 117
  • 1
  • 15
  • 2
    You can increase buffer size for stream copying `$FileStream.CopyTo($Run, 256mb)`. That should make it go faster. – user4003407 Jan 15 '17 at 15:47
  • How do you make it not to cut the file during the process? When I run the second solution, the remote server returns 450 and when I check it out, only a few byes of the original file were uploaded. – Renato Oliveira Aug 16 '21 at 15:23