Amazon AWS Error:配置node.js中缺少凭据

时间:2016-10-13 14:31:45

标签: javascript node.js amazon-web-services amazon-s3 coffeescript

我刚刚开始使用AWS,我尝试使用他们的示例代码here.我使用dotenv将我的密钥存储为环境变量。使用咖啡脚本,我的代码如下所示:

require('dotenv').config()

express = require 'express'
router = express.Router()

AWS = require('aws-sdk')
AWS.config.region = 'us-west-2'

s3bucket = new (AWS.S3)(params: Bucket: 'new-bucket-name')

s3bucket.createBucket ->
  params = 
    Key: process.env.AWS_ACCESS_KEY_ID
    Body: 'Hello!'
  s3bucket.upload params, (err, data) ->
    if err
      console.log 'Error uploading data: ', err
    else
      console.log 'Successfully uploaded data to myBucket/myKey'
    return
  return

但我一直收到以下错误:

message: 'Missing credentials in config',
  code: 'CredentialsError',
  errno: 'EHOSTDOWN',
  syscall: 'connect',
  address: '169.254.169.254',
  port: 80,
  time: 2016-10-13T14:14:03.605Z,
  originalError: 
   { message: 'Could not load credentials from any providers',
     code: 'CredentialsError',
     errno: 'EHOSTDOWN',
     syscall: 'connect',
     address: '169.254.169.254',
     port: 80,
     time: 2016-10-13T14:14:03.605Z,
     originalError: 
      { message: 'Missing credentials in config',
        code: 'CredentialsError',
        errno: 'EHOSTDOWN',
        syscall: 'connect',
        address: '169.254.169.254',
        port: 80,
        time: 2016-10-13T14:14:03.599Z,
        originalError: [Object] } } }

如何解决此问题,是否还需要以某种方式发送我的密钥?

更新: 使用

修复它
AWS.config = new AWS.Config();
AWS.config.accessKeyId = "accessKey";
AWS.config.secretAccessKey = "secretKey";

但现在我收到了这个新错误:

  message: 'Access Denied',
  code: 'AccessDenied',
  region: null,
  time: 2016-10-13T14:38:19.651Z,
  requestId: '958BD7EA261F2DCA',
  extendedRequestId: 'xuBSmGL/GC5Tx1osMh9tBFIwXMLy15VtJXniwYVGutTcoBJgrCeOLZpQMlliF1Azrkmj1tsAX7o=',
  cfId: undefined,
  statusCode: 403,
  retryable: false,
  retryDelay: 11.225715031927086 }

1 个答案:

答案 0 :(得分:2)

Access Denied听起来您的IAM权限设置不正确。检查绑定到这些凭据的用户是否可以在您的帐户中创建存储桶。

通常,AWS软件开发工具包可以读出您的实际ENV变量,因此在这种情况下您可能不需要使用DotEnv。当您将代码推送到可能在EC2或Lambda上运行的生产系统时,您应该真正使用IAM配置文件来处理您的凭据。再说一次.. DotEnv没有必要。