mercredi 25 février 2015

Amazon S3 File Download/Open Error

I am currently using an HTTP handler in an attempt to serve up a file from my S3 bucket. I am not getting an error, but the first run through always fails. The file gets "served" to the webpage and the typical "Open or Save" message appears. If I click "Save", everything works as it should. If I click "Open", however, it simply says that the file could not be downloaded. "Retry" is an option at this point. If I click "Retry" the file opens normally. This happens -EVERY- time. I'm not sure what I'm doing wrong. If anyone has any insight after reviewing my code below, PLEASE help me out.



Dim awsBucket As String = ConfigurationManager.AppSettings("AWSFiles")
Dim awsS3KeyId As String = ConfigurationManager.AppSettings("AWSS3KeyId")
Dim awsS3Key As String = ConfigurationManager.AppSettings("AWSS3Key")
Dim client As AmazonS3Client = New AmazonS3Client(awsS3KeyId, awsS3Key, Amazon.RegionEndpoint.USEast1)
Dim guidFileGUID As New Guid(context.Request.QueryString("GUID"))
Dim guidConv As New GuidConverter
Dim dtFiles As New DataTable

dtFiles = MyData.ProcTable("GetFiles", "@Files", guidConv.ConvertToString(guidFileGUID))

If dtFiles.Rows.Count > 0 Then
Dim reqS3Request As New GetObjectRequest
Dim strOriginalFileName As String = dtFiles(0)("OriginalFileName")
Dim strFileName As String = guidConv.ConvertToString(dtFiles(0)("FileID")) & System.IO.Path.GetExtension(strOriginalFileName)

reqS3Request.BucketName = ConfigurationManager.AppSettings("AWSFiles")
reqS3Request.Key = User.CorpID & "/" & strFileName

Dim respS3Response As GetObjectResponse = client.GetObject(reqS3Request)

Using myStream As System.IO.Stream = respS3Response.ResponseStream
Dim intStreamLength As Integer = myStream.Length
Dim bytFile(intStreamLength - 1) As Byte
Dim intBytesToRead As Integer = intStreamLength
Dim intBytesRead As Integer = 0

Do
Dim intChunkSize = 1000

If intChunkSize > intBytesToRead Then intChunkSize = intBytesToRead

Dim i As Integer = myStream.Read(bytFile, intBytesRead, intChunkSize)

intBytesRead += i
intBytesToRead -= i
Loop While intBytesToRead > 0

context.Response.Buffer = True
context.Response.Clear()
context.Response.ContentType = GetMimeType(strFileName)
context.Response.AddHeader("content-disposition", ("attachment; filename=" & strOriginalFileName))
context.Response.BinaryWrite(bytFile)
context.Response.Flush()
context.Response.Close()
End Using


A bit of background.



  • Majority portion of code found at and modified from http://ift.tt/17kCW7h

  • Files are uploaded to S3 with a new name, which is a GUID. The file information, including original filename, is stored in our database.

  • Sproc "GetFiles" gets the file information stored in our database.

  • Function "GetMimeType" uses a cached datatable to look up the HTTP encoded mime-type for the given file.

  • There used to be a "context.Response.End()" between Flush and Close, but I kept getting an error saying the thread was being aborted.


Here's the standard message that comes up when I click the file link. Initial Standard Download Message Here's the message that comes up when I click "Open" in the previous image. Failed Download Message If I click "Retry" from here, it works just fine.





Aucun commentaire:

Enregistrer un commentaire