amazon-s3 - 使用 Api 网关、Lambda 函数将图像上传到 S3 存储桶

标签 amazon-s3 aws-lambda aws-api-gateway aws-sdk-nodejs

我正在尝试从 postman 上传图像(base64),当我点击无服务器 API 时,我可以看到,在 S3 存储桶中添加了一些东西但没有添加图像,我正在使用 nodejs Lambda 函数,我尝试了很多解决方案,但是那没有用。请建议我哪里错了:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const moment = require('moment');
const fileType = require('file-type');
const sha1 = require('sha1');
const multipart = require('parse-multipart');

exports.handler = function (event, context, callback) {

    let request = event.body;

    // get the request
    let base64String = request.base64String;

    // pass the base64 string into a buffer
    let buffer = new Buffer(base64String, 'base64');

    let fileMime = fileType(buffer);

    // check if the base64 encoded string is a file
    if (fileMime === null) {
        return context.fail('The string supplied is not a file type');
    }

    let file = getFile(fileMime, buffer);
    // let file = getFile(fileMime, parts);
    let params = file.params;

    s3.upload(params, function (err, data) {
    // putObject(params, function (err, data) {
        if (err) {
            console.log(err);
            callback(err);
        }

        // if the file object is uploaded successfully to 
        // s3 then you can get your full url
        console.log('File URL', file.full_path + JSON.stringify(data));
        callback(null, data);

    });
}

let getFile = function (fileMime, buffer) {

    // get the file extension
    let fileExt = fileMime.ext;
    let hash = sha1(new Buffer(new Date().toString()));
    let now = moment().format('YYYY-MM-DD');

    let filePath = hash + '/';
    let fileName = now + '.' + fileExt;
    let fileFullName = filePath + fileName;
    let fileFullPath = 'https://console.aws.amazon.com/s3/buckets/bucket-name/images/' + fileFullName;

    console.log('fileFullPath' + fileFullPath);
    let params = {
        Bucket: 'bucket-name',
        Key: fileFullPath,
        // 'this is simply the filename and the extension, e.g fileFullName + fileExt',
        Body: buffer
    };

    let uploadFile = {
        size: buffer.toString('ascii').length,
        type: fileMime.mime,
        name: fileName,
        full_path: fileFullPath
    }

    return {
        'params': params,
        'uploadFile': uploadFile
    }
}

Response in S3 bucket

when click on any of the response, the complete out is coming as an image

Postman base64 image string

请让我知道,我在哪里遗漏了一些代码。这将不胜感激。

最佳答案

将应在 Web 浏览器中打开的对象上传到 S3 时,您需要设置正确的内容类型。当 S3 为您的对象提供服务时,您设置的内容类型将作为 HTTP header 发送。

网页浏览器使用 MIME types确定如何处理 URL,而不是文件扩展名。

在您的情况下,您传递给 s3.upload 的参数中缺少内容类型.此外, key 不正确。

它应该是:

const params = {
  Bucket: 'bucket-name',
  Key: fileFullName // must be a path within your bucket and not an url like you have in fileFullPath,
  Body: buffer,
  ContentType: fileMime.mime // Sets the content type header, without this your browser cannot infer the type (browsers use mime types, not file extensions)
};

关于amazon-s3 - 使用 Api 网关、Lambda 函数将图像上传到 S3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721634/

相关文章:

angular - AWS S3 桶 "404 Not Found"

hadoop - 使用 Hadoop 读取 s3 时出现 java.lang.NullPointerException(Scalding)

amazon-web-services - ~30 秒后 AWS API 网关超时

c# - 在 ASP.NET Core 2 中强制使用已知 header 的特定大小写

cors - AWS SAM : No 'Access-Control-Allow-Origin' header is present on the requested resource response

amazon-web-services - 如何在浏览器中编辑 AWS S3 中的文件?

amazon-web-services - S3 到 EC2 传输太慢

java - 如何提高从 AWS Lambda (Java) 初始调用 AWS 服务的性能?

amazon-web-services - 为什么我的 lambda 无法与 elasticache 通信?

amazon-web-services - 在AWS API Gateway中-如何为测试调用阶段设置阶段变量?