尖锐[错误:输入文件包含不支持的图像格式]

时间:2019-09-13 11:22:13

标签: node.js amazon-web-services aws-lambda sharp

我正在使用节点的Sharp库进行图像处理,
我正在使用Sharp node 10x版本的AWS Lambda来调整图像的大小,如Sharp库的文档中所建议的那样。

我正在从AWS S3下载图像文件,
到AWS Lambda的/tmp目录,
然后读取/tmp目录中的文件,作为清晰的库代码的输入。

我遇到以下错误-

[Error: Input file contains unsupported image format]

以下是我的示例代码-
1。 handler.js

let service = require('./service');

exports.handler = async (event) => {
    try {
        let requestBody = {};

        console.log('Event =>\n',JSON.stringify(event));

        requestBody.bucketName = 'bucketName';
        requestBody.s3FilePath = 'images/test.jpg';

        requestBody.fileName = requestBody.s3FilePath.substring(requestBody.s3FilePath.lastIndexOf('/')+1,requestBody.s3FilePath.length);
        requestBody.originalFilePath = '/tmp/'+requestBody.fileName;
        requestBody.thumbnailFileName = 'thumb_'+requestBody.fileName;
        requestBody.thumbnailFilePath = '/tmp/'+requestBody.thumbnailFileName;

        console.log('requestBody =>\n',requestBody);

        let serviceResponse = await service.processImage(requestBody);
        if(serviceResponse){
            console.log('Image => ',requestBody.fileName,' processed');
            return true;
        }
        else{
            console.log('Could not process => ',requestBody.fileName);
            return false;
        }
    }
    catch(error){
        console.log('Error =>\n', error);
    }
};

2。 service.js-

let sharp = require('/opt/layer/node_modules/sharp');
let AWS = require('/opt/layer/node_modules/aws-sdk');
let fs = require('fs');

module.exports.processImage = async (requestBody) => {

    let isFileDownload = await downloadImage(requestBody);
    if(!isFileDownload){
        throw('Unable to get Object from S3');
    }

    let isThumbnailCreated = await createThumbnail(requestBody);
    if(!isThumbnailCreated){
        throw('Unable to create thumbnail');
    }

    return true;
};

let downloadImage = async (requestBody) => {
    let client = new AWS.S3({
        region: 'us-east-1'
    });

    var params = {
        Bucket: requestBody.bucketName, 
        Key: requestBody.s3FilePath
    };

    var file = await fs.createWriteStream(requestBody.originalFilePath);
    await client.getObject(params).createReadStream().pipe(file);

    return true;
};

let createThumbnail = async (requestBody) => {
    await sharp(requestBody.originalFilePath)
    .resize({
        width: '320',
        height: '320',
        fit: sharp.fit.outside
    })
    .sharpen()
    .toFile(requestBody.thumbnailFileName)
    .then(info => { 
        console.log('Image processing success response =>\n',info);
     })
    .catch(error => {
        console.log('Image processing failure response =>\n',error);
    });

    return true;
};

这是我的工作代码段-

const sharp = require('sharp');

let test = async () => {

    await sharp('pathToFile/test.jpg')
    .resize({
        width: 320,
        height: 320,
        fit: sharp.fit.outside
    })
    .sharpen()
    .toFile('sharp_320x320.jpg')
    .then(info => { 
        console.log(info);
     })
    .catch(err => {
        console.log(err);
    });
}

test();

参考- https://sharp.pixelplumbing.com/en/stable/api-resize/#examples

0 个答案:

没有答案