我一直试图让这个工作2天。我可以从我的节点服务器获取带有我的存储桶凭据和文件信息的签名URL,但是当我尝试使用签名URL上传文件时,我会超时。这是一些代码:
var AWS_ACCESS_KEY = process.env.AWS_ACCESS_KEY;
var AWS_SECRET_KEY = process.env.AWS_SECRET_KEY ;
var S3_BUCKET = process.env.S3_BUCKET;
exports.getS3Credentials = function(req, res) {
aws.config.update({accessKeyId: AWS_ACCESS_KEY, secretAccessKey: AWS_SECRET_KEY});
var s3 = new aws.S3();
// The date is to help avoid overwriting
var s3Key = Date.now() + req.query.file_name;
var s3_params = {
Bucket: S3_BUCKET,
Key: s3Key,
Expires: 60,
ContentType: req.query.file_type,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3_params, function(err, data) {
if (err) {
console.log(err);
} else {
var return_data = {
signed_request: data,
url: 'https://'+S3_BUCKET+'.s3.amazonaws.com/'+s3Key
};
res.json(return_data);
}
});
};
我通过Angular的GET请求调用上述方法:
var fd = new FormData();
$scope.getS3Credentials = function(item){
$http.get('/api/programs/gets3credentials?file_name=' + item.name + '&file_type=' + item.type).then(function(res) {
$scope.program.profileImageURL = res.data.url;
$scope.credentialsUrl = res.data.signed_request;
});
$scope.imageFile = item;
fd.append('file', item);
};
到此为止,一切似乎都很顺利。然后我使用PUT请求上传:
$http.put($scope.credentialsUrl, fd, {
transformRequest:angular.identity,
headers:{'Content-Type':undefined}
}).then(function(d) {
console.log(d);
// Redirect after save
program.$save(function (response) {
// Clear form fields
clearFields();
$location.path('programs/' + response._id);
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
}, function(err){
console.log(err)
});
在此之后,我没有错误,直到它超时。我已经通过几种方式循环尝试上传,但所有回来都会得到相同的结果。权限可能是导致这种情况的原因吗?