How to upload multiple images at a time in s3 bucket using Node JS (Express)

Arun M
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',
    });
  });

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')

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]?

Erykj97

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)
            },
            
            
        })
    })

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;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I upload multiple images using multer in node js?

From Java

How to upload a file into S3 bucket using CloudFormation script?

From Dev

Node js - Configuring aws s3 images on upload

From Dev

How to upload a WAV file to S3 from the /tmp folder in AWS Lambda using Node.js

From Java

How to upload multiple images in Laravel using AWS bucket, and ignore if same image uploaded?

From Dev

Upload multiple file in amazon s3 bucket using java filechooser

From Dev

How to upload image to S3 using Node

From Dev

How to unlink/remove files and images from s3 bucket using nmp papercut?

From Dev

How to unlink/remove files and images from s3 bucket using nmp papercut?

From Dev

How to SELECT * using multiple variables in MySQL with Node.js and Express?

From Dev

AWS Lambda: How to read CSV files in S3 bucket then upload it to another S3 bucket?

From Dev

How to upload multiple images using codeigniter?

From Dev

using boto to upload csv file into Amazon S3 bucket

From Dev

Multiple image upload using Node.js

From Dev

How to upload all files of a specific type to S3 Bucket?

From Dev

How to upload a thumbnail file to a amazon s3 bucket?

From Dev

object exists in AWS S3 bucket in Node.JS

From Dev

Displaying private S3 images in node express app

From Dev

Corrupt/truncated mp4 upload to S3 bucket using NodeJS and AWS S3

From Dev

Corrupt/truncated mp4 upload to S3 bucket using NodeJS and AWS S3

From Dev

Image upload to server in node.js without using express

From Dev

Image upload to server in node.js without using express

From Dev

Upload Images to Amazon S3 using Django

From Dev

Using Amazon S3 on django to upload Images with ImageField

From Dev

How to download all the contents of an S3 bucket and then upload to another bucket?

From Dev

Upload multiple images using AFNetworking

From Dev

How to upload multiple files on AWS S3 and storing the url to Firebase using Angular?

From Dev

How to upload multiple files on AWS S3 and storing the url to Firebase using Angular?

From Dev

How to upload thousands of images to Amazon S3 at Once

Related Related

  1. 1

    How do I upload multiple images using multer in node js?

  2. 2

    How to upload a file into S3 bucket using CloudFormation script?

  3. 3

    Node js - Configuring aws s3 images on upload

  4. 4

    How to upload a WAV file to S3 from the /tmp folder in AWS Lambda using Node.js

  5. 5

    How to upload multiple images in Laravel using AWS bucket, and ignore if same image uploaded?

  6. 6

    Upload multiple file in amazon s3 bucket using java filechooser

  7. 7

    How to upload image to S3 using Node

  8. 8

    How to unlink/remove files and images from s3 bucket using nmp papercut?

  9. 9

    How to unlink/remove files and images from s3 bucket using nmp papercut?

  10. 10

    How to SELECT * using multiple variables in MySQL with Node.js and Express?

  11. 11

    AWS Lambda: How to read CSV files in S3 bucket then upload it to another S3 bucket?

  12. 12

    How to upload multiple images using codeigniter?

  13. 13

    using boto to upload csv file into Amazon S3 bucket

  14. 14

    Multiple image upload using Node.js

  15. 15

    How to upload all files of a specific type to S3 Bucket?

  16. 16

    How to upload a thumbnail file to a amazon s3 bucket?

  17. 17

    object exists in AWS S3 bucket in Node.JS

  18. 18

    Displaying private S3 images in node express app

  19. 19

    Corrupt/truncated mp4 upload to S3 bucket using NodeJS and AWS S3

  20. 20

    Corrupt/truncated mp4 upload to S3 bucket using NodeJS and AWS S3

  21. 21

    Image upload to server in node.js without using express

  22. 22

    Image upload to server in node.js without using express

  23. 23

    Upload Images to Amazon S3 using Django

  24. 24

    Using Amazon S3 on django to upload Images with ImageField

  25. 25

    How to download all the contents of an S3 bucket and then upload to another bucket?

  26. 26

    Upload multiple images using AFNetworking

  27. 27

    How to upload multiple files on AWS S3 and storing the url to Firebase using Angular?

  28. 28

    How to upload multiple files on AWS S3 and storing the url to Firebase using Angular?

  29. 29

    How to upload thousands of images to Amazon S3 at Once

HotTag

Archive