【问题标题】:Internal server error - when upload base64 through aws lambda/api gateway/s3内部服务器错误 - 通过 aws lambda/api gateway/s3 上传 base64 时
【发布时间】:2021-02-04 09:25:57
【问题描述】:

我正在尝试通过 aws lambda 将图像上传到 s3。 我在 Lambda 中使用下面 url 中的代码,我只更改了变量 fileFullPath 和 Bucket 值。
Upload Image into S3 bucket using Api Gateway, Lambda funnction

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,
        ContentType: fileMime.mime
    };

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

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

此 lambda 函数的执行角色:

在 API Gateway 中,我只创建资源和方法“POST”,并且在方法请求部分没有设置任何内容。
我已经对此 POST 方法进行了测试,但发生内部服务器错误:

更新:当前错误来自 cloudwatch

The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined

【问题讨论】:

  • 您是否检查过 cloudwatch 日志中的任何 lambda 错误?
  • Runtime.ImportModuleError: 错误:找不到模块'moment'"
  • 所以你知道它为什么会失败。我不知道 moment 模块是什么,但您似乎没有在 lambda 包或 lambda 层中提供它。
  • 我应该安装依赖吗?因为在我关注的stackoverflow帖子中,它使用了相同的代码
  • 它们可能具有相同的代码,但依赖关系作为 lambda 层提供或与主代码捆绑在一起。

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


【解决方案1】:

对于刚开始使用或学习 aws lambda 的人,
你可以去 cloudwatch 看日志,
其中包含实际错误和“console.log(xxx)”的日志

【讨论】:

    猜你喜欢
    • 2019-12-21
    • 2018-11-09
    • 2023-02-16
    • 1970-01-01
    • 2019-10-26
    • 2019-12-10
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多