2

I am trying to create and load an object into an s3 container but the PUT event does not seem to create the object

const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3();

exports.handler = async (event) => {
    // I've tried the full arn as well as just the bucket name 
    const bucket ='someBucketName';
    const key = 'sample.csv';
    // for testing purposes I am just loading a 30 row csv file.
    // I've also tried a json object with no luck
    // ex: const data = JSON.stringify({'label': 'foo'});

    const data = fs.readFileSync('trees.csv','utf-8');

    const params ={
        Bucket : bucket,
        Key : key,
        Body: data
    };
    await s3.putObject(params, function (err, data) {
        if(err){
            console.log(`Error creating file ${err.stack}`);
        }else{
            console.log('File Created');
        }
    });
};

When I execute the lambda it runs with no errors. But no file is ever created in the bucket and I never see the output of 'File Created'. If I add a console.log() prior to the put I can see the params are all set. The lambda has full s3 access as well as full access to cloudWatch. This is just a test so I am running it directly from the lambda console in aws.

Any suggestions on what I am missing or doing wrong?

1 Answer 1

3

Found the answer for those interested. I omitted the promise() on the putObject function.

const AWS = require('aws-sdk');
const fs = require('fs');
const s3 = new AWS.S3();

exports.handler = async (event) => {
    const bucket ='someBucketName';
    const key = 'sample.csv';    
    const data = fs.readFileSync('trees.csv','utf-8');

    const params ={
        Bucket : bucket,
        Key : key,
        Body: data
    };
    await s3.putObject(params, function (err, data) {
        if(err){
            console.log(`Error creating file ${err.stack}`);
        }else{
            console.log('File Created');
        }
    }).promise();
};
Sign up to request clarification or add additional context in comments.

1 Comment

Did the API change some how bc I am getting: TypeError: Cannot read properties of undefined (reading 'promise')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.