我正在使用 s3 multer 模块将用户上传的图像文件直接上传到我的存储桶中。该应用程序显示更新成功,但我在存储桶中看不到我的文件。上传文件后的页面响应上传成功。下面是我正在使用的 app.js 文件和 index.html,这是从 github repo 派生的,人们说它可以工作,但我的不工作,请告诉我错误?我在我的本地主机上运行它。
代码:
aws = require('aws-sdk'),
var express = require('express'),
bodyParser = require('body-parser'),
multer = require('multer'),
s3 = require('multer-s3');
aws.config.update({
secretAccessKey:'XXXXX',
accessKeyId:'YYYYY',
region: 'us-west-2'});
var app = express();
app.use(bodyParser.json());
var upload = multer({
storage: s3({
dirname: '/profilepics',
bucket: 'XXXX',
secretAccessKey:'YYYY',
accessKeyId:'TYYYYYY',
region: 'us-west-2',
filename: function (req, file, cb) {
cb(null, "1234"); //use Date.now() for unique file keys
}
})
});
//open in browser to see upload form
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
//use by upload form
app.post('/upload', upload.array('upl'), function (req, res, next) {
res.send("Uploaded!");
});
app.listen(4000, function () {
console.log('Example app listening on port 3000!');
});
索引.html:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
Hey! Lets try uploading to s3 directly :)
<form method="post" enctype="multipart/form-data" action="/upload">
<p>
<input type="text" name="title" placeholder="optional title"/>
</p>
<p>
<input type="file" name="upl"/>
</p>
<p>
<input type="submit"/>
</p>
</form>
</body>
</html>