我正在尝试使用ng-file-upload模块将文件上传到S3。我收到了一个错误:
根据政策无效:政策条件失败:[“eq”, “$ succes_action_status”,“201”]
我的代码基于此particular example,略有变化:
api.js:
exports.signing = function(req, res) {
var request = req.body;
var fileName = request.filename;
var path = fileName;
var readType = "public-read";
var expiration = moment().add(5, 'm').toDate();
var s3Url = process.env.S3_URL;
var s3Policy = {
'expiration': expiration,
'conditions': [{
'bucket' : process.env.S3_BUCKET,
},
['starts-with', '$key', path],
{
'acl': readType
},
{
'succes_action_status': '201'
},
['starts-with', '$Content-Type', request.type],
['content-length-range', 2048, 10485760],
]
};
var stringPolicy = JSON.stringify(s3Policy);
var base64Policy = new Buffer(stringPolicy, 'utf-8').toString('base64');
//sign policy
var signature = crypto.createHmac('sha1', process.env.AWS_SECRET_KEY).update(new Buffer(base64Policy, 'utf-8')).digest('base64');
var credentials = {
url: s3Url,
fields: {
key: fileName,
AWSAccessKeyId: process.env.AWS_ACCESS_KEY,
acl: readType,
policy: base64Policy,
signature: signature,
'Content-Type': request.type,
success_action_status: '201'
}
};
res.jsonp(credentials);
};
controller.js
$scope.upload = function(files) {
if (files.length > 0) {
var filename = files[0].name;
var type = files[0].type;
var query = {
filename: filename,
type: type
}
};
if (files && files.length) {
for (var i=0; i < files.length; i++) {
var file = files[i];
photoService.signingS3(query).success(function(result) {
Upload.upload({
url: "https://***.s3.amazonaws.com",
method: "POST",
fields: result.fields,
transformRequest: function (data, headersGetter) {
//Headers change here
var headers = headersGetter();
delete headers['Authorization'];
return data;
},
file: file
}).progress(function(evt) {
console.log('progress: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
}).error(function(err) {
console.log(err);
});
}).error(function(data, status, headers, config) {
console.log(data);
});
}
}
}
你能告诉我它有什么问题吗?
答案 0 :(得分:0)
此处['starts-with', '$key', path]
路径的值必须是:&#39; s3&#39;中的某个目录+ fileName; 。此外,检查您的桶名称是否相应填写。