Search code examples
node.jsamazon-web-servicesamazon-s3aws-sdkaws-sdk-js

Uploading to S3 Bucket with NodeJS Callback must be a function. Received undefined


Trying to upload to a S3 bucket in NodeJS but I keep getting this error:

##[error]Callback must be a function. Received undefined
##[error]Node run failed with exit code 1

Here is my script doing the uploading:

var AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01', accessKeyId: AWS_SECRET_ID, secretAccessKey:AWS_SECRET_KEY, region:AWS_REGION  });
  var body = fs.open(`./${package}`);

  const params = {
   Body: body,
   Bucket: bucketName
   };
   s3.upload(params, function(err, data){
     if(err){
       console.log(`Failed upload to ${bucketName}`);
       throw err;
     } else {
       console.log(`Succesful upload to ${bucketName}`);
     }
   });

I am confused here cause I do have the callback in upload? And a lot of this was just copied and pasting from AWS examples.


Solution

  • fs.open() takes an argument, callback. This is likely the source of the error.

    This function is an asynchronous function and therefore the returned data needs to be handled by the callback function.

    You'll want to do:

    fs.open(`./${package}`, (err, data) => {
       // Implement callback here
    });
    

    NodeJS Documentation