简体   繁体   中英

creating s3 bucket and folders inside it using node js in aws lambda function

I am both new to Node js and AWS. I am trying to create a bucket in S3 using node js in lambda function. Consequently, I am trying to create folders inside this S3 bucket.

I followed all the questions answered before and tried different iterations of code, but none of them seem to be working. Following is my code which is executing without giving any issues, yet the bucket and the folders are not getting created.

const AWS = require('aws-sdk');

let s3Client = new AWS.S3({
  accessKeyId: '<access_key_id>',
  secretAccessKey: '<secret_access_key>'
});

var params = {
  Bucket : 'pshycology06'
};

exports.handler = async (event, context, callback) => {

// call spaces to create the bucket
    s3Client.createBucket(params, function(err, data) {
      if (err) {
        console.log("\r\n[ERROR] : ", err);
      } else {
        console.log("\r\n[SUCCESS] : data = ",data);
      }
    });
};

The code for creating folders inside the Lambda function is as following --

var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var s3Client = new AWS.S3({apiVersion: '2006-03-01'});

exports.handler = async (event, context) => {

    let params1 = { Bucket: 'travasko', Key: '2/dir1/dir2', Body:'body does not matter' };

    s3Client.putObject(params1, function (err, data) {
        if (err) {
            console.log("Error creating the folder: ", err);
        } else {
            console.log("Successfully created a folder on S3");
        }
    });

Both of them doesn't work. I read a lot of documents on this issue and answers previously asked, but none of them are working for me.

The lambda function has a timeout of 1 minute. It has following policies for the IAM role - 1. AmazonRDSFullAccess 2. AmazonS3FullAccess 3. AWSLambdaVPCExecutionRole

The VPC security group is the default one.

Also, when I am trying to create the same bucket using the following AWS CLI command, it creates the bucket.

aws s3api create-bucket --bucket psychology06 --region us-east-1

I am not sure, where am i making a mistake.

确保不存在同名的存储桶。如果可能,请共享日志。

You need to chain the .promise() method to your aws-sdk calls and await on them because you are creating async functions.

await s3Client.createBucket(params).promise();
await s3Client.putObject(params1).promise();

Furthermore, S3 doesn't work with directories although you may be thrown over by the way the S3 console looks like when you add / to your filenames. You can read more about it here

As you are new, always try aws cli(not recommended) and then search for search for equivalent sdk function while implementing.As it(your code) is async it won't wait until the call back function executes , so you can try something like below.(This is not actual solution , it just tells how to wait until the call back does it's work.)

    'use strict'

    var AWS = require('aws-sdk');
    AWS.config.region = 'us-east-1';
    var s3Client = new AWS.S3({ apiVersion: '2006-03-01' });

    exports.handler = async (event, context) => {
        let params1 = { Bucket: 'travasko', Key: '2/dir1/dir2', Body: 'body does not matter' };
        try {
            let obj = await something(params1);
            callback(null, obj);
        }
        catch (err) {
            callback('error', err)
        }
    }

    async function something(params1) {
        return new Promise(async (resolve, reject) => {
            await s3Client.putObject(params1, function (err, data) {
                if (err) {
                    console.log('Error creating the folder:', err);
                    reject('error during putObject');
                } else {
                    console.log('success' + JSON.stringify(data));
                    resolve('success');
                }
            });
        });
    }

To your question in the comments : Hi Vinit , let me give you little background , the question you have asked is very generic. Firstly VPC is something which you create where you will have your organization private and public subnets that are used to run your ec2 or any hosted services (non-managed services by aws). But as lambda is managed service it runs in aws vpc , they usually take your code and lambda configurations and execute the code.Now coming to your question if we attach vpc in your lambda configurations ( only if your lambda needs to use services hosted in your vpc, else don't use it) then as we discussed lambda runs in aws vpc , so during cold start it created an ENI(think of it as elastic IP) and tries to communicate with your VPC. Before Re-invent an ENI was created for each lambda that was the reason it takes time for the first time and lambda used to time out even though your execution takes lesser time. Now after re-invent EIP's are created per subnet per security group. So now coming to your question when u have attached vpc, if lambda execution is taking more time or not working as expected, then you have to see how your vpc(configs, routes, subnets) is set up and it's very hard to answer as so many parameters are involved unless we debug. So short answer do not attach vpc if your function(code) does not need to talk to any of your own managed instances in vpc (usually private subnet ) etc.

Since, you are using async functionality. Thus, you have to use await on calling "s3Client.createBucket". Then resolve the received promise.

For creating folders, use trailing "/". For example "pshycology06/travasko/".

Do post error logs if these doesn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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