如何使用Node JS(Express)在s3存储桶中一次上传多张图像 [英] How to upload multiple images at a time in s3 bucket using Node JS (Express)

查看:95
本文介绍了如何使用Node JS(Express)在s3存储桶中一次上传多张图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

const s3 = new AWS.S3({
  accessKeyId: process.env.AWS_S3_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_S3_SECRET_ACCESS_KEY
});

const upload = multer({
  storage: multerS3({
    s3: s3,
    Bucket: process.env.AWS_S3_BUCKET_NAME,
    ACL: "public-read",
    metadata: (req, file, cd) => {
      cd(null, { fieldName: file.fieldname });
    },
    key: (req, file, cb) => {
      const ext = path.extname(file.originalname);
      const uniqueName = `${uuid()}${ext}`;
      cb(null, uniqueName);
    },
  }),
});

module.exports = {
    upload
} 
router.post("/photo-upload", upload.array('photos'), (req, res) => {  
    return res.status(200).send({
      success: true,
      result: 'Images Uploaded',
    });
  });

添加此代码后,我的代码崩溃并出现以下错误** node_modules/multer-s3/index.js:94情况未定义":抛出新错误(需要存储桶")

After adding this code my code is crashing and getting below errors **node_modules/multer-s3/index.js:94 case 'undefined': throw new Error('bucket is required')

错误:需要存储桶在新的S3Storage中**

Error: bucket is required at new S3Storage**

有什么方法可以一次不使用循环上传多个文件.正文:缓冲区,我可以将其作为[缓冲区,缓冲区]发送吗?

is there any way to upload multiple file at a time not using loop. Body: buffer, can I send it as a [buffer, buffer]?

推荐答案

我建议您考虑使用multer和multerS3库,如下所示.

I would suggest you maybe consider using multer and multerS3 libraries, this would look as follows.

fileUpload.js

    const aws = require("aws-sdk")
    const multer = require("multer")
    const multerS3 = require("multer-s3")
    const uuid = require("uuid").v4
    const path = require("path")

    const s3 = new aws.S3({
        accessKeyId: <secret-id>,
        secretAccessKey: <secret-key>,
        region: <server-region>,
        apiVersion: "2012-10-17"
    })
    
    const upload = multer({
        storage: multerS3({
            s3:s3,
            bucket: <bucket-name>,
            acl: "public-read",
            metadata: (req, file, cd) => {
                cd(null, {fieldName: file.fieldname})
            },
            key: async (req, file, cb) => {
                const ext = path.extname(file.originalname)
                const uniqueName = `${uuid()}${ext}`
                cb(null, uniqueName)
            },
            
            
        })
    })

然后,您将文件导入到您的路线中,并将upload.array添加到要在其上上传图像的路线上

You then import the file into your routes and add upload.array to the route you want to upload images on

imageRoutes.js

const express = require("express");
const router = express.Router();
const upload = require("./fileUpload")
    
    router.post("/", upload.array("image"), (req, res) => {
    res.send("uploaded")
    }

    module.exports = router;

这篇关于如何使用Node JS(Express)在s3存储桶中一次上传多张图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆