nodejs, multer, aws S3
nodejs, multer, aws S3
如何应用 uuid 和日期,以便存储在我的数据库中的文件名和存储在我的 S3 存储桶中的文件名相同?
使用当前的实现,uuid 和日期始终相同,即使 post 是在数小时后制作的。
谁能帮忙,将不胜感激。
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ID,
secretAccessKey: process.env.AWS_SECRET,
region:process.env.AWS_REGION
})
const uid =uuidv4();
const date =new Date().toISOString()
const multerS3Config = multerS3({
s3: s3,
bucket: process.env.AWS_BUCKET,
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname }); },
shouldTransform: true,
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
transforms: [
{
id: 'full',
key: (req, file, cb) => cb(null, file.originalname
+ "-" + `${date}` + "-" + `${uid}` +
"_full.jpg"),
transform: (req, file, cb) => cb(null,
sharp().resize(2000).jpeg({
quality: 50 }))
},
{
id: 'thumb',
key: (req, file, cb) => cb(null,
file.originalname + "-" +
`${date}` + "-" + `${uid}` + "_thumb.jpg"),
transform: (req, file, cb) => cb(null,
sharp().resize(100).jpeg({
quality: 30 }))
},
],
});
const upload = multer({
storage: multerS3Config,
limits: { fieldSize: 25 * 1024 * 1024 },
});
这是我的post请求
router.post(
"/",
[ upload.array("images", config.get("maxImageCount"))],
async (req, res) => {
const paths = await req.files.map((file) => ({ originalName: file.originalname + "-" +
`${date}`
+ "-" + `${uid}`}));
await Post.create({
title: req.body.title,
userId: req.body.userId,
Post_Images: paths.map((x) => ({ images: x.originalName })),
},
{
include: [Post_Image] }).then(
res.status(201).send())
使用当前的实现,文件将同时存储在 db 和 s3 中。
此外,我的另一个问题是使用 multer 和 multer-s3 有什么区别?我尝试使用 multer 将图像 post 发送到 s3,但它不起作用,所以我使用 multer-s3 并且它起作用了。
更新
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ID,
secretAccessKey: process.env.AWS_SECRET,
region:process.env.AWS_REGION
})
function getFilename() {
return new Date().toISOString() + '-' + uuidv4();
}
function getTransforms() {
const fileName = getFilename();
return {
transforms:[
{
id: 'full',
key: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, fname[0] + '-' + fileName + "_full.jpg")},
transform: (req, file, cb) => cb(null,
sharp().resize(2000).jpeg({
quality: 50
}))
},
{
id: 'thumb',
key: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, fname[0] + '-' + fileName + "_thumb.jpg")},
transform: (req, file, cb) => cb(null,
sharp().resize(100).jpeg({
quality: 30
}))
}
],
metadata: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, {
fieldName: file.fieldname,
key: fname[0] + '-' + fileName
});
}}}
const multerS3Config = multerS3({
s3: s3,
bucket: process.env.AWS_BUCKET,
shouldTransform: true,
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
...getTransforms()
});
const upload = multer({
storage: multerS3Config,
limits: { fieldSize: 25 * 1024 * 1024 },
});
这是我的post请求
router.post(
"/",
[ upload.array("images", config.get("maxImageCount"))],
async (req, res) => {
const paths = await req.files.map((file) => ({ images:
file.transforms[0].metadata.key}));
await Post.create({
title: req.body.title,
userId: req.body.userId,
Post_Images: paths,
},
{
include: [Post_Image] }).then(
res.status(201).send())
我的问题是the date and uuid variables both will initialize when node server start, it never changes until your node server restarts
快速修复:
- 保持不变
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ID,
secretAccessKey: process.env.AWS_SECRET,
region: process.env.AWS_REGION
});
-
date
和 uuid
变量都会在节点服务器启动时初始化,它永远不会改变,直到你的节点服务器重新启动,你只需要将它放入一个函数中即可 return 每次新的文件名,
- 此处函数 returns 文件名,扩展名除外
function getFilename() {
return new Date().toISOString() + '-' + uuidv4();
}
- 为转换创建函数并从
getFilename()
函数在两个版本中传递相同的文件名,也在元数据中添加文件名,
function getTransforms() {
const fileName = getFilename();
return {
transforms: [
{
id: "full",
key: (req, file, cb) => {
let fname = file.originalname.split(".")
cb(null, fname[0] + '-' + fileName + "_full.jpg")
},
transform: (req, file, cb) => cb(null, sharp().resize(2000).jpeg({ quality: 50 }))
},
{
id: "thumb",
key: (req, file, cb) => {
let fname = file.originalname.split(".")
cb(null, fname[0] + '-' + fileName + "_thumb.jpg")
},
transform: (req, file, cb) => cb(null, sharp().resize(100).jpeg({ quality: 30 }))
}
],
metadata: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, {
fieldName: file.fieldname,
key: fname[0] + '-' + fileName + ".jpg"
});
}
}
}
- 调用
getTransforms()
函数,这将 return 转换和 matadata 属性
- 我不确定您登录以将文件名存储在数据库中,
- 从我们从元数据传递的
transfoems.metadata
中获取名称,这将 return 只有 _全名,
router.post("/", async (req, res) => {
const multerS3Config = multerS3({
s3: s3,
bucket: process.env.AWS_BUCKET,
shouldTransform: true,
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
...getTransforms()
});
const upload = multer({
storage: multerS3Config,
limits: { fieldSize: 25 * 1024 * 1024 }
});
upload.array("images", config.get("maxImageCount"))(req, res, async(error) => {
const paths = await req.files.map((file) => ({
images: file.transforms[0].metadata.key
}));
await Post.create({
title: req.body.title,
userId: req.body.userId,
Post_Images: paths,
}, {
include: [Post_Image]
})
});
})
基本上声明 multer-s3 Streaming multer storage engine for AWS S3. 足以区分 multer 和 multer-s3.
The code has not been tested, you can workaround and see what's happen!
如何应用 uuid 和日期,以便存储在我的数据库中的文件名和存储在我的 S3 存储桶中的文件名相同?
使用当前的实现,uuid 和日期始终相同,即使 post 是在数小时后制作的。
谁能帮忙,将不胜感激。
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ID,
secretAccessKey: process.env.AWS_SECRET,
region:process.env.AWS_REGION
})
const uid =uuidv4();
const date =new Date().toISOString()
const multerS3Config = multerS3({
s3: s3,
bucket: process.env.AWS_BUCKET,
metadata: function (req, file, cb) {
cb(null, { fieldName: file.fieldname }); },
shouldTransform: true,
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
transforms: [
{
id: 'full',
key: (req, file, cb) => cb(null, file.originalname
+ "-" + `${date}` + "-" + `${uid}` +
"_full.jpg"),
transform: (req, file, cb) => cb(null,
sharp().resize(2000).jpeg({
quality: 50 }))
},
{
id: 'thumb',
key: (req, file, cb) => cb(null,
file.originalname + "-" +
`${date}` + "-" + `${uid}` + "_thumb.jpg"),
transform: (req, file, cb) => cb(null,
sharp().resize(100).jpeg({
quality: 30 }))
},
],
});
const upload = multer({
storage: multerS3Config,
limits: { fieldSize: 25 * 1024 * 1024 },
});
这是我的post请求
router.post(
"/",
[ upload.array("images", config.get("maxImageCount"))],
async (req, res) => {
const paths = await req.files.map((file) => ({ originalName: file.originalname + "-" +
`${date}`
+ "-" + `${uid}`}));
await Post.create({
title: req.body.title,
userId: req.body.userId,
Post_Images: paths.map((x) => ({ images: x.originalName })),
},
{
include: [Post_Image] }).then(
res.status(201).send())
使用当前的实现,文件将同时存储在 db 和 s3 中。
此外,我的另一个问题是使用 multer 和 multer-s3 有什么区别?我尝试使用 multer 将图像 post 发送到 s3,但它不起作用,所以我使用 multer-s3 并且它起作用了。
更新
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ID,
secretAccessKey: process.env.AWS_SECRET,
region:process.env.AWS_REGION
})
function getFilename() {
return new Date().toISOString() + '-' + uuidv4();
}
function getTransforms() {
const fileName = getFilename();
return {
transforms:[
{
id: 'full',
key: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, fname[0] + '-' + fileName + "_full.jpg")},
transform: (req, file, cb) => cb(null,
sharp().resize(2000).jpeg({
quality: 50
}))
},
{
id: 'thumb',
key: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, fname[0] + '-' + fileName + "_thumb.jpg")},
transform: (req, file, cb) => cb(null,
sharp().resize(100).jpeg({
quality: 30
}))
}
],
metadata: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, {
fieldName: file.fieldname,
key: fname[0] + '-' + fileName
});
}}}
const multerS3Config = multerS3({
s3: s3,
bucket: process.env.AWS_BUCKET,
shouldTransform: true,
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
...getTransforms()
});
const upload = multer({
storage: multerS3Config,
limits: { fieldSize: 25 * 1024 * 1024 },
});
这是我的post请求
router.post(
"/",
[ upload.array("images", config.get("maxImageCount"))],
async (req, res) => {
const paths = await req.files.map((file) => ({ images:
file.transforms[0].metadata.key}));
await Post.create({
title: req.body.title,
userId: req.body.userId,
Post_Images: paths,
},
{
include: [Post_Image] }).then(
res.status(201).send())
我的问题是the date and uuid variables both will initialize when node server start, it never changes until your node server restarts
快速修复:
- 保持不变
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ID,
secretAccessKey: process.env.AWS_SECRET,
region: process.env.AWS_REGION
});
-
date
和uuid
变量都会在节点服务器启动时初始化,它永远不会改变,直到你的节点服务器重新启动,你只需要将它放入一个函数中即可 return 每次新的文件名, - 此处函数 returns 文件名,扩展名除外
function getFilename() {
return new Date().toISOString() + '-' + uuidv4();
}
- 为转换创建函数并从
getFilename()
函数在两个版本中传递相同的文件名,也在元数据中添加文件名,
function getTransforms() {
const fileName = getFilename();
return {
transforms: [
{
id: "full",
key: (req, file, cb) => {
let fname = file.originalname.split(".")
cb(null, fname[0] + '-' + fileName + "_full.jpg")
},
transform: (req, file, cb) => cb(null, sharp().resize(2000).jpeg({ quality: 50 }))
},
{
id: "thumb",
key: (req, file, cb) => {
let fname = file.originalname.split(".")
cb(null, fname[0] + '-' + fileName + "_thumb.jpg")
},
transform: (req, file, cb) => cb(null, sharp().resize(100).jpeg({ quality: 30 }))
}
],
metadata: (req, file, cb) => {
let fname = file.originalname.split(".");
cb(null, {
fieldName: file.fieldname,
key: fname[0] + '-' + fileName + ".jpg"
});
}
}
}
- 调用
getTransforms()
函数,这将 return 转换和 matadata 属性 - 我不确定您登录以将文件名存储在数据库中,
- 从我们从元数据传递的
transfoems.metadata
中获取名称,这将 return 只有 _全名,
router.post("/", async (req, res) => {
const multerS3Config = multerS3({
s3: s3,
bucket: process.env.AWS_BUCKET,
shouldTransform: true,
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
...getTransforms()
});
const upload = multer({
storage: multerS3Config,
limits: { fieldSize: 25 * 1024 * 1024 }
});
upload.array("images", config.get("maxImageCount"))(req, res, async(error) => {
const paths = await req.files.map((file) => ({
images: file.transforms[0].metadata.key
}));
await Post.create({
title: req.body.title,
userId: req.body.userId,
Post_Images: paths,
}, {
include: [Post_Image]
})
});
})
基本上声明 multer-s3 Streaming multer storage engine for AWS S3. 足以区分 multer 和 multer-s3.
The code has not been tested, you can workaround and see what's happen!