Amazon S3: WebException thrown with SendFailure status - vb.NET

I am developing a service that will upload / download files to and from AWS S3 cloud service. The following code works fine, but I get the error " Internet Exception with the status" Submission failed was dropped "or" Connected connection was closed: an unexpected error occurred while submitting "most of the time. Below is a list of links that I have checked. None of them seem to solve the problem. Any help would be appreciated.

The links I have checked:


Download code:

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

      


Download code:

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

      

+3


source to share





All Articles