Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

multer-s3

Package Overview
Dependencies
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multer-s3

Streaming multer storage engine for AWS S3

  • 3.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
142K
decreased by-11.5%
Maintainers
2
Weekly downloads
 
Created

What is multer-s3?

The multer-s3 package is a middleware for handling multipart/form-data, which is primarily used for uploading files. It integrates with AWS S3 to store files directly in an S3 bucket.

What are multer-s3's main functionalities?

Basic File Upload

This code demonstrates a basic file upload to an S3 bucket using multer-s3. It configures AWS SDK with credentials, sets up multer to use S3 as the storage engine, and defines an endpoint to handle file uploads.

const multer = require('multer');
const multerS3 = require('multer-s3');
const AWS = require('aws-sdk');

AWS.config.update({
  accessKeyId: 'your-access-key-id',
  secretAccessKey: 'your-secret-access-key',
  region: 'your-region'
});

const s3 = new AWS.S3();

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'your-bucket-name',
    key: function (req, file, cb) {
      cb(null, Date.now().toString() + '-' + file.originalname);
    }
  })
});

app.post('/upload', upload.single('file'), (req, res) => {
  res.send('File uploaded successfully!');
});

Custom File Naming

This code sample shows how to customize the file naming convention when uploading to S3. The file name is prefixed with the user's ID and a timestamp.

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'your-bucket-name',
    key: function (req, file, cb) {
      const fileName = `${req.user.id}/${Date.now().toString()}-${file.originalname}`;
      cb(null, fileName);
    }
  })
});

Setting Metadata

This code demonstrates how to set metadata for the files being uploaded to S3. The metadata function allows you to add custom metadata to the uploaded files.

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'your-bucket-name',
    metadata: function (req, file, cb) {
      cb(null, { fieldName: file.fieldname });
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString() + '-' + file.originalname);
    }
  })
});

Other packages similar to multer-s3

Keywords

FAQs

Package last updated on 31 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc