简体   繁体   中英

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

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

You can try this

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

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

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