尝试将文件从 lambda tmp 文件夹上传到 s3 存储桶

时间:2021-01-14 12:08:58

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

我是 lambda 函数的新手,我正在尝试从 lambda 函数将文件上传到 s3 存储桶。 创建多个 zip 文件到 tmp 文件夹,然后我想将该 zip 文件上传到 s3 存储桶,我已授予访问 s3 存储桶到 lambda 函数的权限,它没有显示任何错误 我尝试了不同的方法来解决这个问题,但无法修复它。


import fs from 'fs';
import AWS from 'aws-sdk';
import delay from "delay";
const s3 = new AWS.S3({
    accessKeyId: "***",
    secretAccessKey: "***",
    region: "***"
});


const uploadFullfile = () =>{
// reg ex to match
var re = /\.zip$/;

// ensure that this file is in the directory of the files you want to run the cronjob on
fs.readdir("/tmp/",function(err: any, files: any) {
    if (err) {
        console.log( "Could not list the directory.", err);
        process.exit( 1 )
    }

    var matches = files.filter( function(text: any) { return re.test(text) } )
    var numFiles = matches.length;
    if ( numFiles ) {
        // Read in the file, convert it to base64, store to S3

        for(let i = 0; i < numFiles; i++ ) {
            uploadCandidate(matches[i])
        }

    }

})


const uploadCandidate = (fileName:any) => {
  fs.readFile('/tmp/'+fileName, async(err:any, data:any) => {
     console.log("entry",fileName);
     if (err) throw err;
     
     console.log("params")
    await s3.putObject({
         Bucket: 'testbucket', // pass your bucket name
         Key: fileName, // file will be saved as testBucket/contacts.csv
         ContentType: 'application/zip',
         Body: data,
      },function (resp) {
        console.log('Done');
      });
  //delay(1000);
  //fs.unlink('/tmp/'+fileName, function(){
  //    console.log("deleting file");
  //  console.log('/tmp/'+fileName);
  //});
 
    
});
}
 
}

export default uploadFullfile;


我没有收到任何错误,并且我已授予访问 s3 存储桶的权限 我得到的输出

2021-01-14T17:22:38.354+05:30   2021-01-14T11:52:38.354Z *** INFO entry state_fullfile_2021-01-14-11:01:03_part0.zip

2021-01-14T17:22:38.354+05:30   2021-01-14T11:52:38.354Z *** INFO params

2021-01-14T17:22:38.375+05:30   2021-01-14T11:52:38.374Z *** INFO entry association_fullfile_2021-01-14-11:01:03_part5.zip

2021-01-14T17:22:38.375+05:30   2021-01-14T11:52:38.375Z *** INFO params

2021-01-14T17:22:38.378+05:30   2021-01-14T11:52:38.378Z *** INFO entry remark_table_fullfile_2021-01-14-11:01:03_part1.zip

2021-01-14T17:22:38.378+05:30   2021-01-14T11:52:38.378Z **** INFO params

2021-01-14T17:22:38.394+05:30   END RequestId: ****

2021-01-14T17:22:38.394+05:30   REPORT RequestId: *** Duration: 83.91 ms Billed Duration: 84 ms Memory Size: 1024 MB Max Memory Used: 322 MB

2 个答案:

答案 0 :(得分:0)

您是否尝试过增加 lambda 函数时间并尝试过?

答案 1 :(得分:0)

此问题是由于 VPC 端点的权限问题造成的。这是解决方案

new-vpc-endpoint-for-amazon-s3

相关问题