2015-06-03 44 views
2

我有一個表格來上傳文件到我的AWS S3存儲桶,這是形式上傳到S3迴歸故宮

<form action="/user/fileUpload" enctype="multipart/form-data" class="ui form" method="post"> 

    <div class="field"> 
    <select class="ui dropdown"> 
     <option value="">Print Agency</option> 
     <option value="fastprint">Fast Print</option> 
     <option value="printpress">Print Press</option> 
    </select> 
    </div> 

    <div class="field"> 
    <input type="file" name="uploadFile" /> 
    </div> 

    <div class="field"> 
    <input type="submit" value="Upload" class="ui blue submit button" /> 
    </div> 

</form> 

,這是將積極的,如果我按操作提交按鈕

fileUpload: function(req, res) { 
req.file('uploadFile').upload({ 
    adapter: require('skipper-s3'), 
    key: 'THE-KEY', 
    secret: 'THE-SECRET', 
    bucket: 'THE-BUCKET' 
}, function (err, filesUploaded) { 
    if (err) { 
    console.log(err); 
    return res.negotiate(err); 
    } 
    return res.ok({ 
    files: filesUploaded, 
    textParams: req.params.all() 
    }); 
});} 

這是我的S3 CORS配置

<?xml version="1.0" encoding="UTF-8"?> 
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> 
    <CORSRule> 
     <AllowedOrigin>*</AllowedOrigin> 
     <AllowedMethod>GET</AllowedMethod> 
     <AllowedMethod>PUT</AllowedMethod> 
     <AllowedMethod>POST</AllowedMethod> 
     <AllowedMethod>DELETE</AllowedMethod> 
     <MaxAgeSeconds>3000</MaxAgeSeconds> 
     <AllowedHeader>*</AllowedHeader> 
    </CORSRule> 
</CORSConfiguration> 

,這是我的S3鬥政策

{ 
    "Version": "2012-10-17", 
    "Id": "Policy1433335661532", 
    "Statement": [ 
     { 
      "Sid": "Stmt1433335656754", 
      "Effect": "Allow", 
      "Principal": "*", 
      "Action": [ 
       "s3:DeleteObject", 
       "s3:GetObject", 
       "s3:PutObject" 
      ], 
      "Resource": [ 
       "arn:aws:s3:::BUCKET-NAME/*", 
       "arn:aws:s3:::BUCKET-NAME" 
      ] 
     } 
    ] 
} 

但總是導致禁止。我不知道爲什麼。 結果圖像:

http://imgur.com/MMtingu.

,我真的需要在這裏你的幫助。

使用答案

思考@湯姆回答後更新我發現我多麼愚蠢我後,我知道這一切,因爲我忘了幾號線。

我應該把這個線內routes.js

'post /user/fileUpload': { 
controller: 'UserController', 
action: 'fileUpload'}, 

而且提交按鈕下面這一行,因爲我使用CSRF

<input type="hidden" name="_csrf" value="<%= _csrf %>" /> 

而此行內部UserController的行動文件上傳,波紋管「桶」

ACL: 'public-read' 

Regards,

Elmer

+0

你看到你的任何東西console.log(err)? – Meeker

+0

@Meeker:沒有從console.log(err)..看來問題是S3權限。我已經授予每個人都可以列出和更新/刪除的權限。我也已經添加了COSR和桶策略 –

回答

1

如果console.log(err)確實爲null,如您在註釋中提到的那樣,這很可能不是s3的問題,因爲沒有理由讓sails返回403。

相反,我會建議檢查fileUpload是否曾被稱爲。如果不是,請確保策略設置正確,並且您可以訪問該控制器的方法。

+0

'fileUpload'確實被調用。最後,當用戶提交表單時,url更改爲http:// localhost:1337/user/uploadFile。這是不是意味着行爲'fileUpload'被調用?而policies.js已經是''*':true' –

+0

這可能會返回錯誤的唯一方法是通過err對象,它實際上不能爲null。 – tom

+0

那麼,當我提交表格時,我沒有看到任何改變。在我的終端上顯示的唯一一件事是我做了'帆升降機'之後的事情。 無論如何,thx爲您的迴應,至少我不必考慮任何錯誤的權利? –