0

我正在尝试在imagemin将图像上传到我的 S3 存储桶之前对其进行压缩。为此,我有以下代码:

let files = null;
if (contentType === 'image/png') {
    files = await imagemin([fileName], {
        plugins: [
            imageminPngquant({
                quality: [0.5, 0.6],
            }),
        ],
    });
} else if (contentType === 'image/jpeg') {
    files = await imagemin([fileName], {
        plugins: [imageminJpegtran()],
    });
}

const imageData = _.get(files, ['0', 'data'], null);

// Setting up S3 upload parameters
const params = {
    Bucket: process.env.S3_BUCKET_NAME,
    Key: awsFileName, 
    Body: imageData,
    ContentType: contentType,
};

// Uploading files to the bucket
const data = await s3.upload(params).promise();

虽然这可行,但我注意到图像的文件大小与原始图像完全相同 - 这让我相信正在上传原始图像,而不是压缩的图像。

4

0 回答 0