我尝试通过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;
我尝试了所有的方法,但它不工作。而且,在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]
}
1条答案
按热度按时间mv1qrgav1#
如果您正在使用aws API网关,请执行以下操作
步骤-1
我们需要在API网关中启用CORS,因此单击资源,然后单击操作按钮并启用CORS。在CORS设置中,Access-Control-Allow-Headers的值为"",Access-Control-Allow-Origin的值为"",其他设置保持不变。单击启用CORS并替换现有的CORS标头。
第2步现在,转到你正在使用的API的设置。在设置中启用二进制媒体类型。设置该字段的值如下/,并保存更改。