簡體   English   中英

如何在使用 multer 上傳到 S3 之前調整圖像大小

[英]How to Resize Image before upload to S3 using multer

這是我的代碼,它工作正常,我只是希望在 s3 amazon cloud 之前調整圖像大小。

import multer from 'multer';
import path from 'path';
import multerS3 from 'multer-s3';
import aws from 'aws-sdk';

const storageType = {
    s3: multerS3({
        s3: new aws.S3(),
        bucket: 'restsystemplatesimage',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        acl: 'public-read',
        key: (req, file, cb) => {
            console.log(file);
            //const restCod = req.params.data.split('|')
            cb(null, file.originalname);
        }
    })
}

export default {
    storage: storageType['s3'],
    limits: {
        filesize: 1 * 1024 * 1024
    },
    fileFilter: (req, file, cb) => {
        const allowedMimes = [
            "image/jpeg",
            "image/jpg",
            "image/png"
        ];
        if (allowedMimes.includes(file.mimetype)) {
            cb(null, true);
        }else {
            cb( new Error("Invalid file type"));
        }
    }
};

當我在控制台上打印文件時,它顯示...

{
  fieldname: 'image',
  originalname: 'espetinho-de-carne.jpg',
  encoding: '7bit',
  mimetype: 'image/jpeg'
}

緩沖區 object 不應該出現嗎? 如果我不知道它在哪里,我無法修改圖像

請有人幫忙

我找到了解決方案,而不是使用 package multer-s3,我使用了 package multer-sharp-s3,並對我的代碼進行了這些更改

import multer from 'multer';
import path from 'path';
import multerS3 from 'multer-sharp-s3';
import aws from 'aws-sdk';

const storageType = {
    s3: multerS3({
        s3: new aws.S3(),
        Bucket: 'restsystemplatesimage',
        resize: {
            width: 320,
            height: 192
          },
        ACL: 'public-read',
        key: (req, file, cb) => {
            console.log(file);
            //const restCod = req.params.data.split('|')
            cb(null, file.originalname);
        }
    })
}

export default {
    storage: storageType['s3'],
    limits: {
        filesize: 1 * 1024 * 1024
    },
    fileFilter: (req, file, cb) => {
        const allowedMimes = [
            "image/jpeg",
            "image/jpg",
            "image/png"
        ];
        if (allowedMimes.includes(file.mimetype)) {
            cb(null, true);
        }else {
            cb( new Error("Invalid file type"));
        }
    }
};
´´´

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM