Amazon S3:抛出状态为SendFailure的WebException - vb.NET

时间:2017-04-18 06:27:42

标签: vb.net amazon-web-services amazon-s3

我一直在开发一种服务,可以在AWS S3 Cloud Service上传/下载文件。以下代码运行正常,但我收到错误" Web异常状态发送失败"或" 底层连接已关闭:发送上出现意外错误"通常情况下。请在下面找到我检查过的链接列表。他们似乎都没有解决这个问题。任何帮助将不胜感激。

我检查的链接:

  1. 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/
  2. C# HttpWebRequest The underlying connection was closed: An unexpected error occurred on a send
  3. https://forums.asp.net/t/2037197.aspx?C+HttpWebRequest+The+underlying+connection+was+closed+An+unexpected+error+occurred+on+a+send+
  4. 要上传的代码:

    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
    

    下载代码:

    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
    

0 个答案:

没有答案