2017-03-02 14 views
0
module.exports = function (express, router) { 
    var _ = require('lodash'), 
     aws = require('aws-sdk'), 
     AWSConfig = require('../../config/AWSConfig.json'); 

    router.route('/resource/file/sign') 
     .post(function (req, res) { 
      var bucket = "myTestBucket"; 
      aws.config.update({accessKeyId: AWSConfig.AWS_ACCESS_KEY, secretAccessKey: AWSConfig.AWS_SECRET_KEY}); 

      var s3 = new aws.S3(); 
      var options = { 
       Bucket: bucket, 
       Key: req.body.name, 
       Expires: 60, 
       ContentType: req.body.type, 
       ACL: 'private' 
      }; 

      s3.getSignedUrl('putObject', options, function (err, data) { 
       if (err) { 
        return res.send('Error with S3') 
       } 
       res.json({ 
        signed_request: data, 
        url: 'https://s3.amazonaws.com/' + bucket + '/' + req.body.name 
       }) 
      }) 

     }); 


    return router; 
}; 

此請求返回signed_request並且正常工作。Amazon S3簽名作品被禁止使用

但是,當我嘗試上傳到網址我得到forbidden在我的OPTIONS請求。

我也得到了CORS錯誤:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 403. 

這是我第一次嘗試使用amazon s3這樣的IM希望你們當中有些人可能能夠指導我在正確的方向。

UPDATE

我沒有嘗試編輯權限和CORS標頭現在CORS錯誤是走了,但我仍然禁止上傳。

這裏是我的配置:

桶政策

{ 
    "Version": "2012-10-17", 
    "Statement": [ 
     { 
      "Sid": "AddCannedAcl", 
      "Effect": "Allow", 
      "Principal": "*", 
      "Action": [ 
       "s3:PutObject", 
       "s3:PutObjectAcl" 
      ], 
      "Resource": [ 
       "arn:aws:s3:::learningbank-test/*" 
      ], 
      "Condition": { 
       "StringEquals": { 
        "s3:x-amz-acl": [ 
         "public-read" 
        ] 
       } 
      } 
     } 
    ] 
} 

權限:

enter image description here

控制檯錯誤(在我結束) enter image description here

回答

1

您可以在存儲桶上定義跨源資源共享(CORS)規則。有關詳細信息和示例,請參閱documentation

此外,請確保存儲桶權限允許給定的用戶寫入它。從Introduction to S3 access controlUsing bucket policies開始。

更新

您已經定義在你的水桶政策PutObject補助時canned ACL在請求等於public-read。該信息通過HTTP請求中的s3:x-amz-acl標頭進行傳送。

但是,您在代碼中定義的ACL等於private。嘗試將其設置爲public-read以使其與存儲桶策略聲明相匹配。

+0

我已更新我的問題你可以看看嗎? –

相關問題