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