简体   繁体   中英

Amazon AWS Error: Missing credentials in config node.js

I am just getting started using AWS and I'm trying to use their example code here. I am using dotenv to store my keys as environmental variables. Using coffee script my code looks like this:

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

But I keep getting the following error:

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] } } }

How do I fix this, do I also need to send my secret key somehow?

UPDATE: fixed it using

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

but now I am getting this new error:

  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 }

Access Denied sounds like your IAM Permissions are not setup correctly. Check that user tied to those credentials can create buckets in your account.

Also usually the AWS SDKs can read out of your actual ENV variables so you probably do not need to use DotEnv in this case. And when you push code to production systems that might be running on EC2 or Lambda you should really be using a IAM Profile which handles the credentials for you. So again.. DotEnv isn't necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM