NodeJS multer-s3 图片上传 req.file 未定义

时间:2021-03-17 02:05:39

标签: node.js express multer-s3

我尝试通过 nodejs express 服务器将单个图像文件上传到 S3。 但是,中间件“multer-s3”的请求是未定义的 我将上传配置代码分离到“upload.js”文件并在路由中使用它。

这是我的代码。

// index.js

var express = require('express');
var router = express.Router();
var path = require('path');
var { upload } = require('../lib/upload');

var member = require("./member");
var admin = require("./admin");
var column = require("./column");

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express2' });
});

router.post('/uploadOne', upload.single('img'), (req, res) => {
    console.log(req.file);
    const image = req.file;
    if(image == undefined) {
        res
            .status(400)
            .json({
                responseMsg: 'no image'
            });
    } else {
        res
            .status(201)
            .json({
                responseMsg: 'success',
                file: req.file,
            });
    }
});

// 컬럼 관리자
router.post("/admin/login", admin.isAdmin);
router.get("/admin/verify", admin.verifyToken);
router.post("/admin/body", admin.getBody);

router.use("/api/member", member);
router.use("/api/column", column);

module.exports = router;

// upload.js
const multer = require('multer');
const multerS3 = require('multer-s3')
const AWS = require("aws-sdk");
require('dotenv').config();

AWS.config.update({
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_S3_REGION
});

const s3 = new AWS.S3();

let upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: '************',
        contentType: multerS3.AUTO_CONTENT_TYPE, 
        acl: 'public-read-write',
        metadata: function (req, file, cb) {
            cb(null, {fieldName: file.fieldname});
        },
        key: (req, file, cb) => { 
            console.log("success???? " + file);
            cb(null, 'users/' + file.originalname);
        },
    }),
    limits: { fileSize: 12 * 1024 * 1024 },
});

exports.upload = upload;

我尝试了 googleling 提出的所有方法,但没有奏效。 而且,在upload.js 中,key 选项中的日志代码不起作用。 upload.js 文件本身或调用文件时似乎有问题,但我不知道是什么问题。 你能告诉我我的代码有什么问题吗?


为upload.js 编辑1

const multer = require('multer');
const multerS3 = require('multer-s3')
const AWS = require("aws-sdk");
require('dotenv').config();

const endPoint = new AWS.Endpoint(process.env.AWS_S3_ENDPOINT);

// AWS.config.update({
//     endPoint: process.env.AWS_S3_ENDPOINT,
//     accessKeyId: process.env.AWS_ACCESS_KEY_ID,
//     secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
//     region: process.env.AWS_S3_REGION
// });

const s3 = new AWS.S3({
    endPoint: process.env.AWS_S3_ENDPOINT,
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_S3_REGION
});

let upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'bebenity-image',
        contentType: multerS3.AUTO_CONTENT_TYPE, 
        acl: 'public-read',
        // metadata: function (req, file, cb) {
        //     cb(null, {fieldName: file.fieldname});
        // },
        key: (req, file, cb) => {
            console.log("success???? " + file);
            cb(null, file.originalname);
        },
    }),
    limits: { fileSize: 12 * 1024 * 1024 },
});

exports.upload = upload;

编辑上传

的2个日志
Multer {
    storage: S3Storage {
        s3: Service {
            config: [Config],
            isGlobalEndpoint: false,
            endpoint: [Endpoint],
            _events: [Object],
            MONITOR_EVENTS_BUBBLE: [Function: EVENTS_BUBBLE],
            CALL_EVENTS_BUBBLE: [Function: CALL_EVENTS_BUBBLE],
            _clientId: 1
        },
        getBucket: [Function (anonymous)],
        getKey: [Function: key],
        getAcl: [Function (anonymous)],
        getContentType: [Function: autoContentType],
        getMetadata: [Function (anonymous)],
        getCacheControl: [Function (anonymous)],
        getContentDisposition: [Function (anonymous)],
        getStorageClass: [Function (anonymous)],
        getSSE: [Function (anonymous)],
        getSSEKMS: [Function (anonymous)]
    },
    limits: { fileSize: 12582912 },
    preservePath: undefined,
    fileFilter: [Function: allowAll]
}

0 个答案:

没有答案