简体   繁体   English

无法将文件从 LAMBDA 上传到 S3

[英]Can't upload files from LAMBDA to S3

I tested on my localhost, then checked on s3 and saw that there was a new file created.我在本地主机上进行了测试,然后在 s3 上进行了检查,发现创建了一个新文件。 But when testing on Lambda, although there is no error, there is no file on S3.但是在Lambda上测试的时候,虽然没有报错,但是S3上没有文件。 The log of s3.upload(params).promise() is also not displayed. s3.upload(params).promise() 的日志也不显示。

var fs = require('fs');
var AWS = require('aws-sdk');
exports.handler = async (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false
    try {
        AWS.config.update({
            accessKeyId: accessKeyId,
            secretAccessKey: secretAccessKey
        });
        
        var s3 = new AWS.S3();
        var path = 'myfile.txt';
        var file_buffer =  fs.readFileSync(path);

        console.log(file_buffer);
        var params = {
            Bucket: 'bucket-dev',
            Key: '2222.txt',
            Body: file_buffer
        };
        console.log("1111");
        s3.upload(params).promise()
            .then(function(data) {
                console.log("Successfully uploaded to");
                callback(null, "All Good");
            })
            .catch(function(err) {
                console.error(err, err.stack);
                callback(err);
            });
        console.log("2222");
       return context.logStreamName
    } catch (err) {
        console.log(err);
        callback(err);
    }
}

Thanks谢谢

Try not to mix and match async and callback .尽量不要混合和匹配asynccallback Something like this might be closer to what you want...这样的事情可能更接近你想要的......

var fs = require("fs");
var AWS = require("aws-sdk");

exports.handler = async (event, context) => {
  AWS.config.update({
    accessKeyId,
    secretAccessKey,
  });

  const s3 = new AWS.S3();
  const path = "myfile.txt";
  const file_buffer = fs.readFileSync(path);
  const params = {
    Bucket: "bucket-dev",
    Key: "2222.txt",
    Body: file_buffer,
  };
  console.log("1111");
  const res = await s3.upload(params).promise();
  console.log("Successfully uploaded", res);
  return "All good";
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM