AWS Lambda function write to S3

后端 未结 4 1560
星月不相逢
星月不相逢 2020-12-08 06:31

I have a Node 4.3 Lambda function in AWS. I want to be able to write a text file to S3 and have read many tutorials about how to integrate with S3. However, all of them are

4条回答
  •  一向
    一向 (楼主)
    2020-12-08 07:00

    You can upload file on s3 using

    aws-sdk

    If you are using IAM user then you have to provide access key and secret key and make sure you have provided necessary permission to IAM user.

    var AWS = require('aws-sdk');
    AWS.config.update({accessKeyId: "ACCESS_KEY",secretAccessKey: 'SECRET_KEY'});
    var s3bucket = new AWS.S3({params: {Bucket: 'BUCKET_NAME'}});
    function uploadFileOnS3(fileName, fileData){
        var params = {
          Key: fileName,
          Body: fileData,
        };
        s3bucket.upload(params, function (err, res) {               
            if(err)
                console.log("Error in uploading file on s3 due to "+ err)
            else    
                console.log("File successfully uploaded.")
        });
    }
    

    Here I temporarily hard-coded AWS access and secret key for testing purposes. For best practices refer to the documentation.

提交回复
热议问题