AWS SDK : s3.upload is not a function
Asked Answered
C

4

8

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
Cantilena answered 16/6, 2020 at 16:45 Comment(4)
what about other methods ? is it only specific to upload ?Lucinalucinda
console.log(s3.copyObject) gives [ 'length', 'name', 'arguments', 'caller', 'prototype' ] so it is defined. Same thing for console.log(s3.createBucket), it's not undefined. Looks like the problem is specific to upload indeedCantilena
Thanks for the update about putObject, really helped me.Rim
putObject may not work for every case. Ex github.com/aws/aws-sdk-js/issues/2961Rolan
M
2

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

Marnimarnia answered 6/1, 2023 at 6:27 Comment(0)
M
1

You can try this

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

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

Mistrot answered 16/6, 2020 at 16:57 Comment(2)
Thanks, I tried, but the problem remains. s3.upload is still undefined (although other methods like s3.createBucket are not)Cantilena
considering you are able to perform other s3 related operations successfully like listBucket,createBucket,listObjects.Mistrot
M
0

For anyone running into this problem in 2024, the SDK's api has changed and the upload method is available from the @aws-sdk/lib-storage package.

Metagalaxy answered 10/4 at 21:19 Comment(0)
M
0

For uploading images, I ended up using the PutObjectCommand. Here is the AWS Documentation and example code below:

import {S3Client, PutObjectCommand} from '@aws-sdk/client-s3';
const s3 = new S3Client({region: process.env.AWS_REGION});

// base64Image is your encoded image
const buffer = Buffer.from(base64Image, 'base64');

const params = {
    Bucket: 'bucket-name',
    Key: 'file-name.png',
    Body: buffer,
    ContentEncoding: 'base64',
    ContentType: 'image/png',
    ACL: "public-read"
};

const command = new PutObjectCommand(params);
await s3.send(command);
Malfunction answered 29/9 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.