multer s3 file upload
The solution for “multer s3 file upload” can be found here. The following code will assist you in solving the problem.
import ‘dotenv/config.js’
import { MulterModule, FileFieldsInterceptor, FilesInterceptor, FileInterceptor } from ‘@nestjs/platform-express’
import { mimeTypeSupport } from ‘@helpers/helper.mimeType’
import aws from ‘aws-sdk’
import { Request } from ‘express’
import multer from ‘multer’
import multerS3 from ‘multer-s3’
import fs from ‘fs’
aws.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_ACCESS_KEY
})
export class Multer {
private static acl: String = ‘ACL’
static awsStorage: multer.StorageEngine = multerS3({
s3: new aws.S3({
credentials: new aws.Credentials({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_ACCESS_KEY
}),
hostPrefixEnabled: true,
computeChecksums: true,
correctClockSkew: true
}),
bucket: process.env.AWS_BUCKET_NAME,
contentType: multerS3.AUTO_CONTENT_TYPE,
serverSideEncryption: ‘AES256’,
[Multer.acl as any]: ‘public-read’,
metadata(_req: Request, file: Express.Multer.File, done: any) {
if (!file) done(new Error(‘Get file upload failed’), null)
done(null, file)
},
key(_req: Request, file: Express.Multer.File, done: any) {
done(null, `${Date.now()}.${file.originalname.split(‘.’)[1]}`)
}
})
static fileFilter(_req: Request, file: Express.Multer.File, done: any) {
if (!mimeTypeSupport(file.mimetype)) throw new TypeError(‘mimetype not supported’)
if (file.size >= 5242880) throw new TypeError(‘maximum file or image size must be 5 MB or under 5 MB’)
const fileName: string = `${Date.now()}.${file.originalname.split(‘.’)[1]}`
done(null, fileName)
}
}
More questions on [categories-list]
- tss from gene granges
- ixl ansers ixl ansers
- get coin prices node-binance
- how to setup netflix workflow worker
- spritesheets in pyqt spritesheets in pyqt
- cahokia mounds pictures cahokia mounds pictures cahokia mounds pictures
- python 2 decimal places how to get decimal part of a double in python set number of decimals python
- how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python how to find nuber of tweets per day using python
- haskell get specific elements of a String
- vb net code snippets for storing password
- error TS2307: Cannot find module ‘@ngx-meta/core’.
- inline scripts encapsulated in tags