节点将文件上传到S3错误InvalidArgument:null

时间:2020-02-28 03:47:14

标签: node.js amazon-s3 aws-sdk-js

aws-sdk库出现此错误,但我没有找到原因。

我已经尝试了许多可能性,但没有成功,也没有任何线索 状态代码为400,但我已经在网上检查了很多示例,一切对我来说都很好。

这是代码

var AWS = require('aws-sdk');
AWS.config.update({ region: 'nyc3' });

var uuid = require('node-uuid');
var fs = require('fs');

// Create an S3 client
var s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  secretAccessKey: '*****',
  accessKeyId: '*****',
  endpoint: 'https://nyc3.digitaloceanspaces.com',
  region: 'nyc3'
});

AWS.config.region = 'nyc3';

const fileName = 'hello_world.txt';

// Create a bucket and upload something into it
var bucketName = 'my-bucket';
var keyName = fileName;

fs.readFile(fileName, (err, data) => {
  if (err) throw err;
  var params = {
    ACL: 'private',
    Bucket: bucketName,
    Key: keyName,
    Body: JSON.stringify(data, null, 2),
    ContentLength: data.length
  };
  s3.putObject(params, function(err, data) {
    if (err) console.log(err);
    else
      console.log(
        'Successfully uploaded data to ' + bucketName + '/' + keyName
      );
  });

  // s3.upload(params, function(s3Err, data) {
  //   if (s3Err) console.log(s3Err);
  //   else console.log(`File uploaded successfully at ${data.Location}`);
  // });
});

您可以看到我尝试了putObject和upload方法,但两者都存在相同的错误

{ InvalidArgument: null
    at Request.extractError (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/services/s3.js:816:35)
    at Request.callListeners (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/home/antonio/desenvolvimento/projetos/aws-nodejs-sample/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
  message: null,
  code: 'InvalidArgument',
  region: null,
  time: 2020-02-28T03:20:24.289Z,
  requestId: 'tx00000000000016f648fab-005e5886f8-3359f-nyc3b',
  extendedRequestId: undefined,
  cfId: undefined,
  statusCode: 400,
  retryable: false,
  retryDelay: 38.29641987933954 }

我对解决此问题的方法一无所知。 有人可以帮忙吗?

0 个答案:

没有答案