我使用Node SDK将文件上传到Amazon S3。
文件上传工作正常,但我希望将文件的公共网址发送回客户端。
目前我得到的回应是:
Successfully uploaded data { ETag: '"957cd1a335adf5b4000a5101ec1f52bf"' }
这是我的代码。我使用Node Express服务器和Multer来处理上传。
app.use(multer({ // https://github.com/expressjs/multer
dest: './public/uploads/',
limits : { fileSize:100000 },
rename: function (fieldname, filename) {
var time = new Date().getTime();
return filename.replace(/\W+/g, '-').toLowerCase() + time;
},
onFileUploadData: function (file, data, req, res) {
// file : { fieldname, originalname, name, encoding, mimetype, path, extension, size, truncated, buffer }
var params = {
Bucket: creds.awsBucket,
Key: file.name,
Body: data,
ACL: 'public-read'
};
var s3 = new aws.S3()
s3.putObject(params, function (perr, pres) {
if (perr) {
console.log("Error uploading data: ", perr);
} else {
console.log("Successfully uploaded data", pres);
}
});
}
}));
app.post('/upload-image', function(req, res){
if(req.files.file === undefined){
res.send("error, no file chosen");
}
});