Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SDK : s3.upload is not a function

I am trying to upload files to my S3 bucket from my Node.js app, so I am following some very simple tutorials like this one.

The code is pretty straightforward :

const AWS = require("aws-sdk"); // fresh install, version : ^2.697.0

AWS.config.update({ // Credentials are OK
    accessKeyId: process.env.s3_accessKeyId,
    secretAccessKey: process.env.s3_secretAccessKey,
    region: 'eu-central-1'
});

const s3 = new AWS.S3();

let params = {
      // (some upload params, file name, bucket name etc)
 };

s3.upload(params); // <-- crash with error: "s3.upload is not a function"

I had a look at the official AWS documentation and s3.upload() seems to be a thing. I have no idea why I get an error.

If I console.log(s3.upload) I get undefined.

Node.js v13.11.0.

EDIT

I ended up using s3.putObject() which does pretty much the same thing as s3.upload(), and works, while the latter is still inexplicably undefined...

console.log(`typeof s3.upload = `);
console.log(typeof s3.upload); // undefined?? WHY

console.log(`typeof s3.putObject = `);
console.log(typeof s3.putObject); // function, and works
like image 964
Jeremy Thille Avatar asked Jul 01 '26 07:07

Jeremy Thille


2 Answers

Use putObject, example:

s3.client.putObject({
      Bucket: bucketName,
      Key: 'folder/file.txt',
      Body: data,
      ACL: 'public-read'
   }, function (res) {
      console.log('Successfully uploaded file.');
})

Documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property

Can also try reinstalling aws-sdk package. Refer: https://github.com/aws/aws-sdk-js/issues/916#issuecomment-191012462

like image 96
Vallari Agrawal Avatar answered Jul 02 '26 21:07

Vallari Agrawal


You can try this

s3 = new AWS.S3({apiVersion: '2006-03-01'});

s3.upload(params, function(err, data) {
  console.log(err, data);
});

like image 23
Bhagyashri Machale Avatar answered Jul 02 '26 22:07

Bhagyashri Machale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!