2010-07-30 218 views
6

我的一個客戶有一個顯示已從客戶端應用程序上傳的媒體的網站。如何允許客戶端上傳到Amazon S3而不必提供密鑰?

此應用程序最初使用FTP,但我們正在轉向S3以滿足各種數據存儲和性能方面的需求。

我希望能夠做的是讓這個客戶端直接上傳一個文件到我們的中央S3商店(ala dropbox/jungledisk等等),但是我看不到一個這樣做的方式,鍵並將它們嵌入到應用程序中 - 並不理想!

有什麼辦法提供客戶端應用程序與會話密鑰/臨時上傳URL /東西?這可以通過我們的網站api來完成 - 當然這可以完全訪問任何需要的S3密鑰。

對此提出建議?

回答

7

是的,這應該是可能的。你需要做的是創建一個簽署的政策文件每上傳每個用戶。該策略文件,簽名和其他數據必須由客戶端程序使用POST請求發送到您希望他們使用的存儲桶。亞馬遜將檢查請求,檢查參數是否在請求附帶的策略文件的限制範圍內,然後允許該帖子。請注意,政策不應與存儲區政策混淆。實際上,如果您願意,這可以根據請求進行更改,並且由客戶端程序提交(在客戶端程序從您那裏獲得簽名副本之後)。

有關詳細信息,請參見S3文檔的Browser Based Uploads Using POST部分。我建議您詳細閱讀HTML Forms部分,並回顧一下如何向您的客戶端獲取POST參數(對於瀏覽器,您可以將它發送給HTML,這是文檔的措辭,適用於非瀏覽器程序)可能需要某種類型的API調用,然後客戶端向S3提交POST)。

您也可以看看這個網頁,它可以給你一個想法如何設置參數:http://s3.amazonaws.com/doc/s3-example-code/post/post_sample.html

3

其可能使用基於HTML表單的上傳做到這一點:這article沒有很好地解釋瞭如何是可能的。在閱讀article之後,您可以使用下面提到的腳本來簡化生活。

這是一個用於生成我的策略,其簽名和base64字符串的python腳本。

IMP:確保policy.json文件在同一目錄

import base64 
import hmac, hashlib 
import os 
AWS_SECRET_ACCESS_KEY = 'Your Access Key' 

os.system('clear') 
print "This policy generator is for key : " + AWS_SECRET_ACCESS_KEY 
print "Make sure this is the correct key." 
print "IMP: Please be consistent with the file policy.json small changes in space or newline can fail policy" 

policy_document = file("policy.json",'rb').read() 
policy = base64.b64encode(policy_document) 
signature = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY, policy, hashlib.sha1).digest()) 

print 
print "Policy (BASE64_POLICY to be inserted in HTML):-" 
print policy 
print 
print "Signature:-" 
print signature 
print 

,這裏是相應的policy.json文件我用:

{ 
    "expiration": "2014-01-01T00:00:00.00Z", 
    "conditions": [ 
    {"bucket": "BUCKET NAME" }, 
    ["starts-with", "$key", "PREFIX_IF_ANY"], 
    {"acl": "public-read" }, 
    {"success_action_redirect": "http://REDIRECTED_URL" }, 
    ["starts-with", "$Content-Type", "CONTENT_TYPE"], 
    ["content-length-range", 0, 1048576], 
    ] 
} 

的HTML這種代碼的形式是這樣的:

<html> 
    <head> 
     <title>S3 POST Form</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    </head> 

    <body> 
     <form action="http://BUCKET_NAME.s3.amazonaws.com/" method="post" enctype="multipart/form-data"> 
      <input type="hidden" name="key" value="picbum/${filename}"> 
      <input type="hidden" name="AWSAccessKeyId" value="AccessID"> 
      <input type="hidden" name="acl" value="public-read"> 
      <input type="hidden" name="success_action_redirect" value="http://REDIRECTED_URL"> 
      <!-- Fill these HTML fields with data generated from python script --> 
      <input type="hidden" name="policy" value='BASE64_POLICY'> 
      <input type="hidden" name="signature" value="SIGNATURE_GENERATED"> 
      <input type="hidden" name="Content-Type" value="CONTENT_TYPE"> 
      <!-- Include any additional input fields here --> 

      File to upload to S3: 
      <input name="file" type="file"> 
      <br> 
      <input type="submit" value="Upload File to S3"> 
     </form> 
    </body> 
</html> 
+0

這是我的投票,因爲文章解釋每一個。 – Saeger 2015-03-26 18:34:23

相關問題