【问题标题】:S3 bucket: TypeError: Cannot read property 'transfer-encoding' of undefinedS3 存储桶:TypeError:无法读取未定义的属性“传输编码”
【发布时间】:2020-07-18 02:10:12
【问题描述】:

我第一次使用 AWS 的 S3 存储桶。我使用了 node、express server、multer 和 multerS3。为了测试,我使用了邮递员。我想将图像上传到我的 s3 存储桶。我创建了存储桶并将我的凭据添加到我的后端。但是当我尝试使用邮递员上传图片时,(这就是我所做的post request)。我收到错误“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}!`));

【问题讨论】:

标签: node.js express amazon-s3 multer multer-s3


【解决方案1】:

如果singleUploadmulter中间件,我一直是这样使用的:

router.post("/", singleUpload, (req, res) => {
  return res.json({
    imgUrl: req.file.location // this should be path?
  });
});

另外,我认为没有 location 属性。也许path 是你要找的?

fieldname       Field name specified in the form    
originalname    Name of the file on the user's computer 
encoding        Encoding type of the file   
mimetype        Mime type of the file   
size            Size of the file in bytes   
destination      The folder to which the file has been saved    DiskStorage
filename        The name of the file within the destination DiskStorage
path            The full path to the uploaded file  DiskStorage
buffer          A Buffer of the entire file MemoryStorage

【讨论】:

    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多