将文件上传到Amazon S3 [英] Upload File to Amazon S3

查看:1198
本文介绍了将文件上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我解释一下我的要求.

Let me explain my requirement..

  1. 我想将(由客户端)上传到我的应用程序的文件存储到Amazon S3.
  2. 文件大小〜1-10 MB
  3. 但是,客户端接口必须是REST API 由我的应用程序提供.因此,在解析文件上传(HTTP POST)请求之后,我的应用程序必须将文件存储在S3中.
  4. 因此,在上传到S3之前,我必须将文件临时存储在磁盘上.
  1. I want to store files uploaded to my app (by clients) to Amazon S3..
  2. File Size ~ 1-10 MB
  3. However, the client interface has to be a REST API provided by my application. Consequently, after parsing file upload (HTTP POST) request, my application must store the file in S3.
  4. As a result, I have to store file temporarily on disk before uploading to S3..

有解决方法吗?我可以取消服务器上的临时文件存储吗.如果不清楚,请告知我..

Is there a workaround? Can I do away with temporary file store on my server.. Please let me know if I am not clear..

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

EDIT - Is it OK to get byte array from FileItem object and store it rather than the file itself..?

推荐答案

您的整个想法是避免I/O,对吗?您无需在上传之前保存文件,只需将字节数组发送到Amazon REST API.

Your whole idea is to avoid I/O right ? you don't need to save the file before doing the upload, you could simple send the array of bytes to amazon REST API.

这是我的示例VB.NET代码,可同时上传和下载:

Here is my sample VB.NET code that do both upload and download:

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

这篇关于将文件上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆