A plugin multer to transform image and upload to AWS S3
npm install multer-sharp-s3-opt
yarn test
`
Importing
$3
`javascript
const s3Storage = require('multer-sharp-s3');
const storage = s3Storage(options);
`
$3
`typescript
import * as s3Storage from 'multer-sharp-s3';
const storage = s3Storage(options);
`
Usage
`javascript
const express = require('express');
const multer = require('multer');
const s3Storage = require('multer-sharp-s3');
const aws = require('aws-sdk');
aws.config.update({
secretAccessKey: config.uploads.aws.secretAccessKey, // Not working key, Your SECRET ACCESS KEY from AWS should go here, never share it!!!
accessKeyId: config.uploads.aws.accessKeyId, // Not working key, Your ACCESS KEY ID from AWS should go here, never share it!!!
region: config.uploads.aws.region, // region of your bucket
})
const s3 = new aws.S3()
const app = express();
// without resize image
const storage = s3Storage({
s3,
Bucket: config.uploads.aws.Bucket,
Key: ${config.uploads.aws.Bucket}/test/${Date.now()}-myImage,
ACL: config.uploads.aws.ACL,
})
const upload = multer({ storage: storage })
app.post('/upload', upload.single('myPic'), (req, res) => {
console.log(req.file); // Print upload details
res.send('Successfully uploaded!');
});
// or
// single resize without Key
const storage2 = gcsSharp({
s3,
Bucket: config.uploads.aws.Bucket,
ACL: config.uploads.aws.ACL,
resize: {
width: 400,
height: 400
},
max: true
});
const upload2 = multer({ storage: storage2 });
app.post('/uploadandresize', upload2.single('myPic'), (req, res, next) => {
console.log(req.file); // Print upload details
res.send('Successfully uploaded!');
});
/* If you need generate image with specific size
* simply to adding multiple: true property and
* resize must be an array and must be include suffix property
* and suffix has a special value that is 'original'
* it will no transform image, just upload the image as is
* example below with Key as callback function
*/
const storage = s3Storage({
Key: (req, file, cb) => {
crypto.pseudoRandomBytes(16, (err, raw) => {
cb(err, err ? undefined : raw.toString('hex'))
})
},
s3,
Bucket: config.uploads.aws.Bucket,
multiple: true,
resize: [
{ suffix: 'xlg', width: 1200, height: 1200 },
{ suffix: 'lg', width: 800, height: 800 },
{ suffix: 'md', width: 500, height: 500 },
{ suffix: 'sm', width: 300, height: 300 },
{ suffix: 'xs', width: 100 },
{ suffix: 'original' }
],
});
const upload = multer({ storage });
app.post('/uploadmultiplesize', upload.single('myPic'), (req, res, next) => {
console.log(req.file); // print output
res.send('Successfully uploaded!');
});
/*
* If the directory property exists,
* the suffix property is ignored and
* inserted separated by Bucket's directory.
*/
const storage3 = s3Storage({
Key: (req, file, cb) => {
crypto.pseudoRandomBytes(16, (err, raw) => {
cb(err, err ? undefined : raw.toString('hex'))
})
},
s3,
Bucket: config.uploads.aws.Bucket,
multiple: true,
resize: [
{ suffix: 'lg', directory: 'large', width: 800, height: 800 }, // insert BUCKET/large/filename
{ suffix: 'md', directory: 'medium', width: 500, height: 500 }, // insert BUCKET/medium/filename
{ suffix: 'sm', directory: 'small', width: 300, height: 300 }, // insert BUCKET/small/filename
],
});
const upload3 = multer({ storage3 });
app.post('/uploadmultiplesize', upload3.single('myPic'), (req, res, next) => {
console.log(req.file); // print output
res.send('Successfully uploaded!');
});
// also can upload any file (non image type)
const storage = s3Storage({
s3,
Bucket: config.uploads.aws.Bucket,
Key: ${config.uploads.aws.Bucket}/test/${Date.now()}-myFile,
ACL: config.uploads.aws.ACL,
// resize or any sharp options will ignore when uploading non image file type
resize: {
width: 400,
height: 400,
},
})
const upload = multer({ storage })
app.post('/uploadfile', upload.single('myFile'), (req, res, next) => {
console.log(req.file); // print output
res.send('Successfully uploaded!');
});
`
for more example you can see here
#### Multer-Sharp-S3 options
multer sharp s3 is inherit from s3 upload property putObjectRequest.
Below are special / custom options from this package
| option | default | value | role |
| ------ | ------- | ----- | ---- |
| S3 | no | object | instance from AWS.S3 class. it mus be specify |
| Key | randomString | string or function | your s3 Key |
| Bucket | no | string | Required your bucket name on AWS S3 to upload. Environment variable - AWS_BUCKET |
| ACL | 'public-read' | string | Required acl credentials file for AWS S3 |
| multiple | false | boolean | for multiple resize to work |
| resize | no | object or Array