Results 1 to 6 of 6

Thread: [RESOLVED] Read 4gb file into FileStream and copy to MemoryStream

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Resolved [RESOLVED] Read 4gb file into FileStream and copy to MemoryStream

    VS2015, .Net 4.0
    Used NuGet to get the latest AWSSDK.S3 v3.3.5.3 and AWSSDK.Core v3.3.8 references.

    I'm trying to use the AWS SDK to upload a file from our server to an Amazon S3 account which works just fine for files 2gb or smaller but one client we generate a file that when it's gzipped ends up being around 3.71gb and our upload routine errors out because File.ReadAllBytes() only handles up to 2gb.

    I've tried creating the FileStream manually and using .CopyTo() to copy it into the MemoryStream before passing it into the AWK SDK PutObjectRequest, but I'm still getting this exception:
    Code:
    System.IO.IOException: Stream was too long.
       at AmazonS3Utilities.UploadFile.UploadFile(String FilePath, String BucketPath, String UsrName, String Pass, RegionEndpoint EndPt) in C:\TFS\DataTeam Projects\Utilities\AmazonS3Utilities\AmazonS3Utilities\UploadFile.vb:line 123
       at AmazonS3Utilities.UploadFile.UploadFile(String FilePath, String BucketPath) in C:\TFS\DataTeam Projects\Utilities\AmazonS3Utilities\AmazonS3Utilities\UploadFile.vb:line 90
       at UploadFileS3.Main.Main(String[] Args) in C:\TFS\DataTeam Projects\Utilities\AmazonS3Utilities\UploadFileS3\Main.vb:line 48
    Here's my vb.net code:
    vb.net Code:
    1. Public Function UploadFile(FilePath As String, BucketPath As String, UsrName As String, Pass As String, EndPt As RegionEndpoint) As Boolean
    2.         Dim Output As Boolean = False
    3.  
    4.         UsrName = UsrName.Trim()
    5.         Pass = Pass.Trim()
    6.         BucketPath = BucketPath.Trim()
    7.         FilePath = FilePath.Trim()
    8.  
    9.         If UsrName.Length > 0I Then
    10.             If Pass.Length > 0I Then
    11.                 If File.Exists(FilePath) Then
    12.                     Dim s3Client As AmazonS3Client = Nothing
    13.                     Dim memStream As MemoryStream = Nothing
    14.                     Dim fileStream As FileStream = Nothing
    15.                     Try
    16.                         s3Client = New AmazonS3Client(UsrName, Pass, EndPt)
    17.                         'memStream = New MemoryStream(File.ReadAllBytes(FilePath))
    18.                         memStream = New MemoryStream
    19.                         fileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
    20.                         fileStream.CopyTo(memStream)
    21.  
    22.                         Dim rr As New PutObjectRequest() With {.BucketName = BucketPath, .Key = Path.GetFileName(FilePath), .InputStream = memStream}
    23.                         Dim response As PutObjectResponse = s3Client.PutObject(rr)
    24.  
    25.                         Output = response.HttpStatusCode = HttpStatusCode.OK
    26.                     Catch ex As Exception
    27.                         Throw ex
    28.                     Finally
    29.                         If fileStream IsNot Nothing Then
    30.                             fileStream.Close()
    31.                             fileStream.Dispose()
    32.                         End If
    33.  
    34.                         If memStream IsNot Nothing Then
    35.                             memStream.Close()
    36.                             memStream.Dispose()
    37.                         End If
    38.  
    39.                         If s3Client IsNot Nothing Then
    40.                             s3Client.Dispose()
    41.                         End If
    42.                     End Try
    43.                 Else
    44.                     Throw New FileNotFoundException($"File {Path.GetFileName(FilePath)} was not found")
    45.                 End If
    46.             Else
    47.                 Throw New ArgumentNullException("Password cannot be blank or all spaces")
    48.             End If
    49.         Else
    50.             Throw New ArgumentNullException("UserName cannot be blank or all spaces")
    51.         End If
    52.  
    53.         Return Output
    54.     End Function
    I do see that .Net offers a BufferedStream, could I somehow use the FileStream.CopyTo() into a BufferedStream that will then .CopyTo() the MemoryStream?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Read 4gb file into FileStream and copy to MemoryStream

    no need....
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    The CopyTo has a buffer size parameter .... so what you'll want to do is simply loop until you hit the EOS (end of stream) each time specifying a buffer size.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Read 4gb file into FileStream and copy to MemoryStream

    Quote Originally Posted by techgnome View Post
    no need....
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    The CopyTo has a buffer size parameter .... so what you'll want to do is simply loop until you hit the EOS (end of stream) each time specifying a buffer size.

    -tg
    Is there an example showing this?
    I have a severe lack of working with stream so this all seems confusing to me still.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Read 4gb file into FileStream and copy to MemoryStream

    actually... now that I look at the code closer... you're taking the file stream, copying it to a memory stream and then passing the memory stream to the request call... any reason for that? InputStream should be able to accept any IStream ... including a FileStream object... it should then be responsible for buffering and reading the stream directly.

    what if you change this:
    Code:
                            s3Client = New AmazonS3Client(UsrName, Pass, EndPt)
                            'memStream = New MemoryStream(File.ReadAllBytes(FilePath))
                            memStream = New MemoryStream
                            fileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
                            fileStream.CopyTo(memStream)
     
                            Dim rr As New PutObjectRequest() With {.BucketName = BucketPath, .Key = Path.GetFileName(FilePath), .InputStream = memStream}
                            Dim response As PutObjectResponse = s3Client.PutObject(rr)
    to this:

    Code:
                            s3Client = New AmazonS3Client(UsrName, Pass, EndPt)
                            fileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
     
                            Dim rr As New PutObjectRequest() With {.BucketName = BucketPath, .Key = Path.GetFileName(FilePath), .InputStream = fileStream}
                            Dim response As PutObjectResponse = s3Client.PutObject(rr)
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Read 4gb file into FileStream and copy to MemoryStream

    Quote Originally Posted by techgnome View Post
    actually... now that I look at the code closer... you're taking the file stream, copying it to a memory stream and then passing the memory stream to the request call... any reason for that? InputStream should be able to accept any IStream ... including a FileStream object... it should then be responsible for buffering and reading the stream directly.

    what if you change this:
    Code:
                            s3Client = New AmazonS3Client(UsrName, Pass, EndPt)
                            'memStream = New MemoryStream(File.ReadAllBytes(FilePath))
                            memStream = New MemoryStream
                            fileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
                            fileStream.CopyTo(memStream)
     
                            Dim rr As New PutObjectRequest() With {.BucketName = BucketPath, .Key = Path.GetFileName(FilePath), .InputStream = memStream}
                            Dim response As PutObjectResponse = s3Client.PutObject(rr)
    to this:

    Code:
                            s3Client = New AmazonS3Client(UsrName, Pass, EndPt)
                            fileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
     
                            Dim rr As New PutObjectRequest() With {.BucketName = BucketPath, .Key = Path.GetFileName(FilePath), .InputStream = fileStream}
                            Dim response As PutObjectResponse = s3Client.PutObject(rr)
    -tg
    I just came here to post that I had (eventually) noticed that the parameter it accepts is "Stream" and I am able to pass it the FileStream directly and it works fine.
    The examples I had followed a couple of weeks ago when I first made this all used a MemoryStream and thus showed reading a FileStream into a MemoryStream which kept their examples aligned.

    So my final code is:
    vb.net Code:
    1. Public Function UploadFile(FilePath As String, BucketPath As String, UsrName As String, Pass As String, EndPt As RegionEndpoint) As Boolean
    2.         Dim Output As Boolean = False
    3.  
    4.         UsrName = UsrName.Trim()
    5.         Pass = Pass.Trim()
    6.         BucketPath = BucketPath.Trim()
    7.         FilePath = FilePath.Trim()
    8.  
    9.         If UsrName.Length > 0I Then
    10.             If Pass.Length > 0I Then
    11.                 If File.Exists(FilePath) Then
    12.                     Dim s3Client As AmazonS3Client = Nothing
    13.                     Dim fileStream As FileStream = Nothing
    14.  
    15.                     Try
    16.                         s3Client = New AmazonS3Client(UsrName, Pass, EndPt)
    17.  
    18.                         fileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
    19.  
    20.                         Dim rr As New PutObjectRequest() With {.BucketName = BucketPath,
    21.                                                                .Key = Path.GetFileName(FilePath),
    22.                                                                .InputStream = fileStream
    23.                                                               }
    24.                         Dim response As PutObjectResponse = s3Client.PutObject(rr)
    25.  
    26.                         Output = response.HttpStatusCode = HttpStatusCode.OK
    27.                     Catch ex As Exception
    28.                         Throw ex
    29.                     Finally
    30.                         If fileStream IsNot Nothing Then
    31.                             fileStream.Close()
    32.                             fileStream.Dispose()
    33.                         End If
    34.  
    35.                         If s3Client IsNot Nothing Then
    36.                             s3Client.Dispose()
    37.                         End If
    38.                     End Try
    39.                 Else
    40.                     Throw New FileNotFoundException($"File {Path.GetFileName(FilePath)} was not found")
    41.                 End If
    42.             Else
    43.                 Throw New ArgumentNullException("Password cannot be blank or all spaces")
    44.             End If
    45.         Else
    46.             Throw New ArgumentNullException("UserName cannot be blank or all spaces")
    47.         End If
    48.  
    49.         Return Output
    50.     End Function
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Read 4gb file into FileStream and copy to MemoryStream



    sweet! Glad it worked.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width