I have been developing a service that will upload/download files to and from the AWS S3 Cloud Service. The following code runs fine, but I'm getting the error "Web exception with status Send failure was thrown" or "The underlying connection was closed: An unexpected error occurred on a send" more often than not. Please find below the list of links I checked. None of them seem to solve the issue. Any help would be appreciated.
Links I checked:
- https://blogs.msdn.microsoft.com/jpsanders/2009/01/07/you-receive-one-or-more-error-messages-when-you-try-to-make-an-http-request-in-an-application-that-is-built-on-the-net-framework-2-0/
- C# HttpWebRequest The underlying connection was closed: An unexpected error occurred on a send
- https://forums.asp.net/t/2037197.aspx?C+HttpWebRequest+The+underlying+connection+was+closed+An+unexpected+error+occurred+on+a+send+
Code to Upload:
Private Shared Function Upload(ByVal S3Key As String, ByVal bucketName As String, ByRef client As IAmazonS3, ByVal filePath As String) As String
Try
Dim putRequest As New PutObjectRequest
putRequest.BucketName = bucketName
putRequest.Key = S3Key
putRequest.FilePath = filePath
putRequest.Metadata.Add("x-amz-meta-title", "someTitle")
putRequest.Metadata.Add("Entity", "entity")
putRequest.Metadata.Add("DocumentType", "docType")
putRequest.Metadata.Add("UploadDate", DateTime.UtcNow.ToShortDateString())
putRequest.Metadata.Add("Content-Type", "contentType")
Dim response As PutObjectResponse = client.PutObject(putRequest)
Catch amazonS3Exception As AmazonS3Exception
If amazonS3Exception.ErrorCode IsNot Nothing AndAlso (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") OrElse amazonS3Exception.ErrorCode.Equals("InvalidSecurity")) Then
Return "Invalid AWS Credentials"
Else
Return "Error occurred. Message:'{0}' when writing an object" + amazonS3Exception.Message
End If
End Try
End Function
Code to Download:
Public Function AWSFileDownload(ByVal fname As String, ByVal strFileType As Int32) As String
Try
Dim path As Path
Dim data As String
Dim awsAccessKey = System.Configuration.ConfigurationManager.AppSettings("AWSACCESSKEY")
Dim awsSecretKey = System.Configuration.ConfigurationManager.AppSettings("AWSSECRETKEY")
Dim bucketName_Documents = System.Configuration.ConfigurationManager.AppSettings("bucketName_Documents")
Dim bucketName_OtherDocuments = System.Configuration.ConfigurationManager.AppSettings("bucketName_OtherDocuments")
Dim S3Key As String = fname
Dim type As String = ""
Dim dest As String = ""
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Using s3Client = New AmazonS3Client(awsAccessKey, awsSecretKey, RegionEndpoint.USEast1)
Dim request As New GetObjectRequest()
If (strFileType = "0") Then
request.BucketName = bucketName_OtherDocuments
ElseIf (strFileType = "1") Then
request.BucketName = bucketName_Documents
End If
request.Key = S3Key
Using response As GetObjectResponse = s3Client.GetObject(request)
Dim ext = Path.GetExtension(S3Key)
If Not IsDBNull(ext) Then
ext = LCase(ext)
End If
Select Case ext
Case ".htm", ".html"
type = "text/HTML"
Case ".txt"
type = "text/plain"
Case ".doc", ".rtf"
type = "Application/msword"
Case ".csv", ".xls"
type = "Application/x-msexcel"
Case ".docx"
type = "Application/vnd.openxmlformats-officedocument.wordprocessingml.document"
Case ".xlsx"
type = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Case Else
type = "text/plain"
End Select
dest = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), S3Key)
If Not File.Exists(dest) Then
response.WriteResponseStreamToFile(dest)
End If
End Using
End Using
Response.ClearHeaders()
Response.AppendHeader("content-disposition", "attachment; filename=" + S3Key)
If type <> "" Then
Response.ContentType = type
End If
Response.WriteFile(dest)
Response.End()
Catch amazonS3Exception As AmazonS3Exception
If amazonS3Exception.ErrorCode IsNot Nothing AndAlso (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") OrElse amazonS3Exception.ErrorCode.Equals("InvalidSecurity")) Then
Return "Invalid AWS Credentials"
Else
Return "Error occurred. Message:'{0}' when writing an object" + amazonS3Exception.Message
End If
End Try
End Function