我第一次使用 AWS 的 S3 存储桶。我使用了 node、express server、multer 和 multerS3。为了测试,我使用了邮递员。我想将图像上传到我的 s3 存储桶。我创建了存储桶并将我的凭据添加到我的后端。但是当我尝试使用邮递员上传图片时,(这就是我发布请求的方式)。我收到错误“TypeError:无法读取未定义的属性‘传输编码’”。
这是我的 s3 设置
const aws = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");
aws.config.update({
secretAccessKey: "AKIAJWFJ6GS2*******",
accessKeyId: "W/2129vK2eLcwv67J******",
region: "us-east-1"
});
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
bucket: "testing-alak",
metadata: function(req, file, cb) {
cb(null, { fieldName: file.fieldname });
},
key: function(req, file, cb) {
cb(null, Date.now().toString());
}
})
});
module.exports = upload;
这是上传文件设置
const express = require("express");
const router = express.Router();
const upload = require("./upload-file");
const singleUpload = upload.single("image");
router.post("/", (req, res) => {
singleUpload((req, res, next) => {
return res.json({
imgUrl: req.file.location
});
});
});
module.exports = router;
这是我的快递服务器
const express = require("express");
const app = express();
const route = require("./route");
const bodyParser = require("body-parser");
app.use(express.json()); //body Parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/img", route);
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(` App is listening at port ${port}!`));