无法使用节点下载S3对象

无法使用节点下载S3对象

本文介绍了无法使用节点下载S3对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用节点时下载S3对象,并已问过上一个问题.代码如下.我将访问密钥凭据声明为环境变量,并尝试了我的帐户访问密钥以及IAm角色.我尝试生成多个密钥,配置文件,存储桶和文件,以及创建存储桶策略,所有这些都导致了以下堆栈跟踪.我是否正确调用了存储桶和文件对象,以及正确设置了我的凭据?在使用aws-sdk下载对象之前,是否还需要确保先进行其他一些政策或配置设置?谢谢!

I am trying to download an S3 object while using node, and have asked this previous question. Code is below. I am declaring my access key credentials as environment variables and have tried both my account access keys as well as an IAm role. I have tried generating multiple keys, profiles, buckets and files, as well as creating a bucket policy, all of which have resulted in the following stack trace below. Am I calling the bucket and file objects correctly, as well as setting up my credentials correctly? Is there some other policy or set of configurations I need to make sure set up first before using the aws-sdk to download objects? Thanks!

代码:

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)'

推荐答案

此问题是您在创建S3 Client之后设置"config".更改代码,使其看起来像这样:

This problem is that you are setting up "config" after creating the S3 Client. Change your code to look like this:

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);
});

这篇关于无法使用节点下载S3对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 08:28