admin管理员组文章数量:1287912
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
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
Share
Improve this question
edited Jun 17, 2020 at 8:10
Jeremy Thille
asked Jun 16, 2020 at 16:45
Jeremy ThilleJeremy Thille
26.4k12 gold badges47 silver badges64 bronze badges
4
-
what about other methods ? is it only specific to
upload
? – Ersoy Commented Jun 16, 2020 at 17:00 -
console.log(s3.copyObject)
gives[ 'length', 'name', 'arguments', 'caller', 'prototype' ]
so it is defined. Same thing forconsole.log(s3.createBucket)
, it's not undefined. Looks like the problem is specific toupload
indeed – Jeremy Thille Commented Jun 16, 2020 at 17:05 - Thanks for the update about putObject, really helped me. – Roy Levy Commented Dec 21, 2021 at 15:27
- putObject may not work for every case. Ex github./aws/aws-sdk-js/issues/2961 – Akshay Commented Dec 5, 2022 at 12:56
4 Answers
Reset to default 2Use 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./AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
Can also try reinstalling aws-sdk package. Refer: https://github./aws/aws-sdk-js/issues/916#issuement-191012462
You can try this
s3 = new AWS.S3({apiVersion: '2006-03-01'});
s3.upload(params, function(err, data) {
console.log(err, data);
});
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.
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 mand = new PutObjectCommand(params);
await s3.send(mand);
本文标签: javascriptAWS SDKs3upload is not a functionStack Overflow
版权声明:本文标题:javascript - AWS SDK : s3.upload is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741331462a2372788.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论