我正在尝试在使用节点时下载S3对象,并已询问此previous question。代码如下。我将访问密钥凭据声明为环境变量,并尝试了我的帐户访问密钥和IAm角色。我已经尝试生成多个密钥,配置文件,存储桶和文件,以及创建存储桶策略,所有这些都导致下面的堆栈跟踪。我是否正确调用存储桶和文件对象,以及正确设置我的凭据?在使用aws-sdk下载对象之前,是否需要先确定一些其他策略或配置集?谢谢!
代码:
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
AWS.config = new AWS.Config();
AWS.config.accessKeyId = "myAccessKey";
AWS.config.secretAccessKey = "mySecretAccessKey";
AWS.config.region = "us-east-1";
var params = { Bucket: "myBucket", Key: "fileName" }
s3.getObject(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
堆栈追踪:
jemery62:aws/ (aws_edits✗) $ node main.js [21:26:21]
{ AccessDenied: Access Denied
at Request.extractError (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/services/s3.js:577:35)
at Request.callListeners (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
message: 'Access Denied',
code: 'AccessDenied',
region: null,
time: 2017-10-27T02:26:23.394Z,
requestId: 'DDD8D2211A6F4551',
extendedRequestId: 'adUqsuXLIs0EIuy62/9YwcDWiil+ms9fX7+q4GIOxXWos5/5Jgvs3JQsg4N3BchWPnjIjj6Yqjg=',
cfId: undefined,
statusCode: 403,
retryable: false,
retryDelay: 23.592474526046313 } 'AccessDenied: Access Denied\n at Request.extractError (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/services/s3.js:577:35)\n at Request.callListeners (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\n at Request.emit (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/sequential_executor.js:77:10)\n at Request.emit (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:683:14)\n at Request.transition (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:22:10)\n at AcceptorStateMachine.runTo (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/state_machine.js:14:12)\n at /Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/state_machine.js:26:10\n at Request.<anonymous> (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:38:9)\n at Request.<anonymous> (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/request.js:685:12)\n at Request.callListeners (/Users/jemery62/dev/jimmyemery_com/node_modules/aws-sdk/lib/sequential_executor.js:115:18)'
答案 0 :(得分:2)
此问题是您在创建S3客户端后设置“config”。将代码更改为如下所示:
var AWS = require('aws-sdk');
AWS.config = new AWS.Config();
AWS.config.accessKeyId = "myAccessKey";
AWS.config.secretAccessKey = "mySecretAccessKey";
AWS.config.region = "us-east-1";
var s3 = new AWS.S3();
var params = { Bucket: "myBucket", Key: "fileName" }
s3.getObject(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});