将文件上传到Amazon S3

时间:2013-10-29 12:20:01

标签: amazon-s3

让我解释一下我的要求..

  1. 我想将上传到我的应用程序的文件(由客户端)存储到Amazon S3 ..
  2. 文件大小~1-10 MB
  3. 但是,客户端界面必须是REST API 由我的申请提供。因此,在解析文件上载(HTTP POST)请求后,我的应用程序必须将文件存储在S3中。
  4. 因此,我必须暂时将文件存储在磁盘上才能上传到S3 ..
  5. 有解决方法吗?我可以取消服务器上的临时文件存储..如果我不清楚,请告诉我。

    编辑 - 从FileItem对象获取字节数组并存储它而不是文件本身是否可以。?

1 个答案:

答案 0 :(得分:2)

您的整个想法是避免I / O正确吗?在上传之前你不需要保存文件,你可以简单地将字节数组发送到amazon 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