2012-12-26 26 views
7

我正在嘗試關注this "how-to"(表單POST到S3存儲區)並且似乎無法通過我的政策和簽名。政策和簽名問題的導軌發佈表格到s3上傳

我不太確定我的政策和簽名是否有錯?但是我確實知道我正在評估我在助手中創建的方法。我試過將政策&簽名值轉換爲符號<%= :S3_UPLOAD_SIGNATURE %>,我試過<%= h,<%= raw,「#{<%= ..}」。

我已經調用過輔助方法,沒有問題,所以我有點失落。

錯誤:

NameError in Proj_files#new 

Showing /app/views/proj_files/new.html.erb where line #8 raised: 

uninitialized constant ActionView::CompiledTemplates::S3_UPLOAD_POLICY 
Extracted source (around line #8): 

5:  <input type="hidden" name="AWSAccessKeyId" value= <%= ENV['AWS_ACCESS_KEY_ID'] %> > 
6:  <input type="hidden" name="acl" value="private"> 
7:  <input type="hidden" name="success_action_redirect" value="http://localhost/"> 
8:  <input type="hidden" name="policy" value= <%= S3_UPLOAD_POLICY %> > 
9:  <input type="hidden" name="signature" value= <%= S3_UPLOAD_SIGNATURE %> > 
10:  <input type="hidden" name="Content-Type" value="image/png"> 
11:  <!-- Include any additional input fields here --> 

我有一個proj_files控制器與對應的new.html.erb:

<form action="https://s3.amazonaws.com/MY_BUCKET" method="post" enctype="multipart/form-data"> 
    <input type="hidden" name="key" value="uploads/${filename}"> 
    <input type="hidden" name="AWSAccessKeyId" value= <%= ENV['AWS_ACCESS_KEY_ID'] %> > 
    <input type="hidden" name="acl" value="private"> 
    <input type="hidden" name="success_action_redirect" value="http://localhost/"> 
    <input type="hidden" name="policy" value= <%= S3_UPLOAD_POLICY %> > 
    <input type="hidden" name="signature" value= <%= S3_UPLOAD_SIGNATURE %> > 
    <input type="hidden" name="Content-Type" value="image/png"> 
    <!-- 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> 

和proj_files_helper.rb:

module ProjFilesHelper 

    def S3_UPLOAD_POLICY options = {} 
    options[:content_type] ||= '' 
    options[:acl] ||= 'private' 
    options[:max_file_size] ||= 500.megabyte 
    options[:path] ||= '' 

    Base64.encode64(
     "{'expiration': '#{10.hours.from_now.utc.strftime('%Y-%m-%dT%H:%M:%S.000Z')}', 
     'conditions': [ 
      {'bucket': '#{ENV['S3_BUCKET']}'}, 
      ['starts-with', '$key', ''], 
      {'acl': '#{options[:acl]}'}, 
      {'success_action_status': '201'}, 
      ['content-length-range', 0, #{options[:max_file_size]}], 
      ['starts-with','$Content-Type',''] 
     ] 
    }").gsub(/\n|\r/, '') 
    end 


    def S3_UPLOAD_SIGNATURE options = {} 
    Base64.encode64(
     OpenSSL::HMAC.digest(
     OpenSSL::Digest::Digest.new('sha1'), 
     ENV['AWS_SECRET_ACCESS_KEY'], s3_policy(options))).gsub("\n","") 
    end 

end 

感謝看看!

更新: 我改變了方法名稱爲小寫,這讓我有點進一步(我應該意識到這一點!)。

現在,我得到一個錯誤S3:

<Error> 
<Code>AccessDenied</Code> 
<Message> 
Invalid according to Policy: Policy Condition failed: ["eq", "$bucket", "MY_BUCKETS_NAME"] 
</Message> 

看起來有可能是ENV變量$桶壞參考就去找......「MY_BUCKETS_NAME」沒有顯示正確的桶名稱... 。如果任何人都可以提供任何幫助獲得軌道張貼到S3的形式和運行/指出我的錯誤,我會很感激。

感謝

UPDATE2 根據註釋下面我修改表單動作 「https://s3.amazonaws.com/MY_BUCKET」,並收到此錯誤:

Invalid according to Policy: Policy Condition failed: ["eq", "$acl", "public-read"] 

親近...謝謝!

UPDATE3 我正在打架!

我已經修改了策略並形成ACL以具有相同的一致性值(私有或公共讀取)。下面

評論促使我修改表單動作爲:http://MY_BUCKET.s3.amazonaws.com/我得到這個錯誤:

<Code>AccessDenied</Code> 
<Message> 
Invalid according to Policy: Policy Condition failed: ["eq", "$success_action_status", "201"] 
</Message> 

奇怪的是,當我去到AWS S3管理控制檯,將文件上傳到我的水桶,它告訴我的鏈接是'http://s3.amazonaws.com/MY_BUCKET'的形式。我在amazonaws之前和之後添加了MY_BUCKET,仍然收到相同的錯誤...

我不確定哪裏出現錯誤配置...我要創建一個新存儲桶並查看是否設置錯誤....

謝謝!

IT WORKS NOW !!! 我固定的一切,從答案......但後來不得不做出一個更改變......

我的形式進行了「success_action_redirect」領域,但我的政策有一個success_action_status!

政策和表單字段必須匹配! DUH!

感謝您的所有幫助......時間進一步調整!

+0

我猜你的存儲桶名稱不是:MY_BUCKETS_NAME – apneadiving

+0

我用這個替換了錯誤信息存儲桶名稱引用......但錯誤確實引用了我的正確存儲桶。 – twinturbotom

+0

你在url中有相同的存儲桶名稱嗎? 'https:// YOUR_BUCKET_NAME.s3.amazonaws.com /' – apneadiving

回答

6

1 /要上傳文件,您必須使用此URL "http://#{bucket_name}.s3.amazonaws.com/"。它在documentation明確提出:

The action defines the URL that will process the request; this must be set to the bucket's URL. For example, if your bucket's name is "johnsmith", then the URL would be " http://johnsmith.s3.amazonaws.com/ "

2 /你必須有一貫的政策,看來你設置的簽名public-read和形式private

1

我收到以下錯誤:根據政策 無效:策略條件失敗:[\「EQ \」,\「$鬥\」

多小時後,我才知道,你不能有一個大寫字母的桶。將桶改爲小寫固定它。