我正在使用Node SDK將文件上傳到Amazon S3。節點JS AWS S3文件上傳。如何獲得公共URL響應
文件上傳工作正常,但我想獲取文件的公共網址發送回客戶端。
目前我得到的迴應是:
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");
}
});
你如何做這個工作,我得到「錯誤初始化中間件 類型錯誤:app.use()需要中間件功能」的消息 – someUser