S3 bucket: TypeError: Невозможно прочитать свойство 'Transfer-Encoding' из неопределенного - PullRequest
0 голосов
/ 06 апреля 2020

Я впервые использую AWS 'S3 bucket. Я использовал node, express server, multer и multerS3. Для тестирования я использовал почтальон. Я хотел загрузить изображение в мое ведро s3. Я создал корзину и добавил свои учетные данные в свой бэкэнд. Но когда я пытаюсь загрузить изображение с помощью почтальона (так я отправил запрос ). Я получил сообщение об ошибке «Ошибка типа: не удается прочитать свойство« кодировка передачи »неопределенного».

Это моя настройка 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;

Это мой express сервер

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}!`));

1 Ответ

0 голосов
/ 06 апреля 2020

Если singleUpload - это multer промежуточное ПО, я всегда использовал его так:

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
...