使用无服务器框架和connect-multiparty模块将图像从lambda上传到S3后,图像已损坏

时间:2018-09-27 12:23:02

标签: node.js amazon-s3 upload aws-lambda serverless

当我从本地上载图像时,我可以从浏览器查看图像,但是当我使用无服务器框架从lambda上载图像时,图像显示为空白框,表示s3bucket.upload的正文数据在<我尝试转换为base64并缓冲的缓冲区格式,即使没有运气。

const express = require('express');
const bodyParser = require('body-parser');
var Request = require("request");
const AWS = require('aws-sdk');
var fs = require('fs');
const serverless = require('serverless-http');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart({ uploadDir: '/tmp' })

app.post('/uploadImageToS3',multipartMiddleware, (req, res) => {
console.log(req.files);
console.log(req.files.uImage.type);
    if(typeof req.files.uImage !== 'undefined' && req.files.uImage !== null)
    {

       AWS.config.update({
    accessKeyId: '.................',
    secretAccessKey: '................................',
    region: 'ap-south-1'
  });
  var file = req.files.uImage;
  var s3bucket = new AWS.S3();


 fs.readFile(file.path, function (err, data) {

       var params = {
            Bucket: "..........",
            Key:file.name,
            Body:data,
            ContentType: req.files.uImage.type,
            ContentEncoding: 'base64'
        };
console.log(data);
        s3bucket.upload(params, function(err, data) {
            if (err) {
            return res.status(400).json(err);
            console.log('ERROR MSG: ', err);
            } else {
                console.log(data.Location);
                res.status(200).json(data);
            }
        });

});
}else
{
return res.status(400).json('No files were uploaded.');
}

});

0 个答案:

没有答案