您的整个想法是避免 I/O 对吗?您不需要在上传之前保存文件,您可以简单地将字节数组发送到亚马逊 REST API。
这是我上传和下载的示例 VB.NET 代码:
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Net
Imports System.Security.Cryptography
Imports System.Text
Imports System.Threading.Tasks
Module Module1
Sub Main()
Dim obj As New Program
obj.UploadFile()
'obj.DownloadFile() 'Download Example
End Sub
End Module
Class Program
Private Const KeyId As String = "yourkey"
Private Const AccessKey As String = "your/access"
Private Const S3Url As String = "https://s3.amazonaws.com/"
Public Sub DownloadFile()
Dim bucketName As String = "yourbucket"
Dim FileName As String = "file.png"
Dim timeStamp As String = String.Format("{0:r}", DateTime.UtcNow)
Dim stringToConvert As String = Convert.ToString((Convert.ToString((Convert.ToString("GET" & vbLf + vbLf + vbLf + vbLf + "x-amz-date:") & timeStamp) + vbLf + "/") & bucketName) + "/") & FileName
Dim ae = New UTF8Encoding()
Dim signature = New HMACSHA1() With { _
.Key = ae.GetBytes(AccessKey) _
}
Dim bytes = ae.GetBytes(stringToConvert)
Dim moreBytes = signature.ComputeHash(bytes)
Dim encodedCanonical = Convert.ToBase64String(moreBytes)
' Send the request
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(Convert.ToString((Convert.ToString("https://") & bucketName) + ".s3.amazonaws.com" + "/") & FileName), HttpWebRequest)
'request.ContentType = "application/octet-stream";
request.Headers.Add("x-amz-date", timeStamp)
request.Headers.Add("Authorization", "AWS " + KeyId + ":" + encodedCanonical)
request.Method = "GET"
' Get the response
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Dim ReceiveStream As Stream = response.GetResponseStream()
Console.WriteLine(response.StatusCode)
End Sub
Public Sub UploadFile()
Dim fileData = File.ReadAllBytes("C:\file.png")
Dim timeStamp As String = String.Format("{0:r}", DateTime.UtcNow)
Dim stringToConvert As String = (Convert.ToString("PUT" & vbLf + vbLf + "application/octet-stream" & vbLf + vbLf + "x-amz-acl:public-read" + vbLf + "x-amz-date:") & timeStamp) + vbLf + "/celso711/file.png"
'resource
Dim ae = New UTF8Encoding()
Dim signature = New HMACSHA1() With { _
.Key = ae.GetBytes(AccessKey) _
}
Dim bytes = ae.GetBytes(stringToConvert)
Dim moreBytes = signature.ComputeHash(bytes)
Dim encodedCanonical = Convert.ToBase64String(moreBytes)
Dim url = "https://bucket.s3.amazonaws.com/file.png"
Dim request = TryCast(WebRequest.Create(url), HttpWebRequest)
request.Method = "PUT"
request.Headers("x-amz-date") = timeStamp
request.Headers("x-amz-acl") = "public-read"
request.ContentType = "application/octet-stream"
request.ContentLength = fileData.Length
request.Headers("Authorization") = (Convert.ToString("AWS ") & KeyId) + ":" + encodedCanonical
Dim requestStream = request.GetRequestStream()
requestStream.Write(fileData, 0, fileData.Length)
requestStream.Close()
Using response = TryCast(request.GetResponse(), HttpWebResponse)
Dim reader = New StreamReader(response.GetResponseStream())
Dim data = reader.ReadToEnd()
End Using
End Sub
End Class