繁体   English   中英

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

[英]Try to upload file from lambda tmp folder to s3 bucket

我是 lambda function 的新手,我正在尝试将文件从 lambda ZC151C4252748E178 上传到 s3 存储桶 creating multiple zip file to tmp folder and after that I want to upload that zip file to s3 bucket, I have given permission to access s3 bucket to lambda function its not showing any error I tried different approach to resolve this but not able to fixed it .


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 存储桶 output 的权限

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

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

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

新的 vpc-endpoint-for-amazon-s3

暂无
暂无

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

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