javascript AWS SDK:s3.upload不是函数

smdncfj3  于 2023-01-08  发布在  Java
关注(0)|答案(2)|浏览(83)

我正在尝试从我的Node.js应用程序将文件上传到我的S3桶,所以我遵循了一些非常简单的教程,如this one
代码非常简单:

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"

我看了一下AWS的官方文档,s3.upload()似乎是一个东西。我不知道为什么我会得到一个错误。
如果我console.log(s3.upload)我得到undefined
节点. js版本13.11.0。

    • 编辑**

我最终使用了s3.putObject(),它做的事情与s3.upload()几乎相同,并且可以工作,而后者仍然莫名其妙地未定义...

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
yuvru6vn

yuvru6vn1#

使用putObject,示例:

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

dwbf0jvd

dwbf0jvd2#

你可以试试这个

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

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

相关问题