将文件从网站上传到S3

问题描述 投票:0回答:1

我正在尝试将图像(PNG)从我的网站上传到S3。

我正在向Lambda发送multipart / form-data请求,然后使用Busboy解析它。

它上传到S3很好,并显示它是一个图像/ png但如果我下载文件并尝试查看它,该文件无效

可能导致什么?我不知道我在哪里错了。

码:

var AWS = require('aws-sdk');
var BluebirdPromise = require("bluebird");
var Busboy = require('busboy');
var s3 = BluebirdPromise.promisifyAll(new AWS.S3());
var str = require('string-to-stream');

const SavePFP = async((user_id, req) => {
    var busboy = new Busboy({headers: req.headers});

    let res = await(new Promise((resolve) => {
        busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
            console.log('File [' + fieldname + ']: filename: ' + filename);

            file.on('data', function (data) {
                console.log('File [' + fieldname + '] got ' + data.length + ' bytes');

                resolve({
                    filename,
                    encoding,
                    mimetype,
                    data
                });
            });

            file.on('end', function () {
                console.log('File [' + fieldname + '] Finished');
            });
        });

        str(req.rawBody).pipe(busboy);
    }));

    let {data, encoding, mimetype} = res;

    var params = {
        Bucket: '...',
        Key: user_id,
        Body: data,
        ACL: 'public-read',
        ContentEncoding: encoding,
        ContentType: mimetype
    };

    console.log("Uploading to AWS S3...");
    let response = await(s3.upload(params).promise());
    console.log("[SavePFP]: " + JSON.stringify(response, null, 2));

    if (response && response.Location) {
        return response.Location;
    }
});

module.exports = {
    SavePFP
};

提前致谢!

amazon-web-services amazon-s3 aws-lambda aws-sdk aws-sdk-nodejs
1个回答
0
投票

我只是通过将文件转换为base64并从那里上传来完全避免解决这个问题。

谢谢

© www.soinside.com 2019 - 2024. All rights reserved.