📜  nodejs aws s3 上传 - TypeScript 代码示例

📅  最后修改于: 2022-03-11 14:48:36.309000             🧑  作者: Mango

代码示例2
import multer from 'multer'
import multerS3 from 'multer-s3'
import aws from 'aws-sdk'
import { Request } from 'express'

aws.config.update({
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_ACCESS_KEY
})

export class Multer {
    public static upload = multer({
        storage: multerS3({
            s3: new aws.S3(),
            bucket: process.env.AWS_BUCKET_NAME,
            contentType: multerS3.AUTO_CONTENT_TYPE,
            serverSideEncryption: 'AES256',
            acl: 'public-read',
            key: function (request: Request, file: Express.Multer.File, done: any) {
                const fileName: string = `${Date.now().toString()} - ${file.originalname}`
                done(null, fileName)
            }
        }),
        fileFilter: (req: Request, file: Express.Multer.File, done: any) => {
            if (!mimeTypeSupport(file.mimetype)) throw new TypeError('mimetype not supported')
            const fileName: string = `${Date.now().toString()} - ${file.originalname}`
            done(null, fileName)
        }
    }).array('upload', 100)
}