background image

Credentials are the most important thing you need to set when using any AWS SDK. Credentials can be
set globally on the 

AWS.config

 object or per service by passing the credential information to the service

object directly.

There are a few ways to load credentials. Here they are, in order of recommendation:

1. Loaded from environment variables,

2. Loaded from a JSON file on disk,

3. Loaded from EC2 metadata service,

4. Hardcoded in your application

We recommend you not hard-code your AWS credentials in your application; however, it is reasonable
to temporarily hard-code credential information in small personal scripts or for testing purposes.

Credentials from Environment Variables

By default, the AWS SDK for Node.js will automatically detect AWS credentials set in your environment
and use them for requests. This means that if you properly set your environment variables, you do not
need to manage credentials in your application at all.

The keys that the SDK looks for are as follows:

AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN (optional)

Alternately, the SDK can accept the 

AMAZON_

 prefix instead:

AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_ACCESS_KEY, AMAZON_SESSION_TOKEN (optional)

Credentials from Disk

You can also load configuration and credentials from disk using 

AWS.config.loadFromPath

 by passing

a file to a JSON document containing the configuration data. For example, if you had a file named
'config.json' with the contents:

{ "accessKeyId": "akid", "secretAccessKey": "secret", "region": "us-east-1" }

You can load the JSON data using the command:

AWS.config.loadFromPath('./config.json');

Note that the 

loadFromPath

 method clobbers all existing configuration on the object. If you are adding

extra configuration, make sure you add it after this call.

Hard-Coding Credentials

Note

We recommend you not hard-code credentials inside an application. Use this method only for
small personal scripts or for testing purposes.

Version 0.9.1-pre.2 : Preview

5

AWS SDK for Node.js Getting Started Guide

Setting AWS Credentials

Содержание AWS SDK

Страница 1: ...AWS SDK for Node js Getting Started Guide Version 0 9 1 pre 2 Preview...

Страница 2: ...Amazon Web Services AWS SDK for Node js Getting Started Guide...

Страница 3: ...azon Route 53 Amazon S3 Amazon VPC In addition Amazon com graphics logos page headers button icons scripts and service names are trademarks or trade dress of Amazon in the U S and or other countries A...

Страница 4: ...AWS SDK for Node js 1 AWS Account and Credentials 2 Configuration Guide 4 Services 7 Making Requests 9 Examples 14 Version 0 9 1 pre 2 Preview 4 AWS SDK for Node js Getting Started Guide...

Страница 5: ...g into a terminal window npm install aws sdk Note Installing the aws sdk npm package on Windows may display errors while trying to install the optional dependency for libxmljs This error can be safely...

Страница 6: ...n a copy of the License at http www apache org licenses LICENSE 2 0 Unless required by applicable law or agreed to in writing software distributed under the License is distributed on an AS IS BASIS WI...

Страница 7: ...r Secret Access Key click Show Your secret key must remain a secret that is known only to you and AWS Keep it confidential in order to protect your account Store it securely in a safe place and never...

Страница 8: ...with new settings The most common settings are accessKeyId secretAccessKey sessionToken for credential management region to set the region for requests sslEnabled whether SSL is enabled or not maxRet...

Страница 9: ...redentials in your application at all The keys that the SDK looks for are as follows AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN optional Alternately the SDK can accept the AMAZON_ prefi...

Страница 10: ...service object constructor var ec2 new AWS EC2 region ap southeast 2 maxRetries 15 Note that the constructor takes all of the same configuration data as the AWS config object described above including...

Страница 11: ...aPipeline AWS DirectConnect AWS DynamoDB AWS EC2 AWS ElastiCache AWS ElasticBeanstalk AWS ElasticTranscoder AWS ELB AWS EMR AWS Glacier AWS IAM AWS ImportExport AWS OpsWorks AWS RDS AWS Redshift AWS R...

Страница 12: ...obal settings For example an EC2 object can be created for a specific region var ec2 new EC2 region us west 2 This object will continue to use the globally provided credentials Passing Arguments to a...

Страница 13: ...tances function error data if error console log error an error occurred else console log data request succeeded The error and data parameters are described in the Response Object section below Note th...

Страница 14: ...null if an error occurs see below The error property In the event of a service error or transfer error the response error property will be filled with the given error data in the form code SHORT_UNIQU...

Страница 15: ...re passing parameters to the operation the callback should be placed after the parameters s3 client getObject Bucket bucket Key key function err data AWS Request Events You can alternatively register...

Страница 16: ...id s3 client listBuckets fail function error response console log error or console log response error send Prints code Forbidden message null on complete function response The complete event triggers...

Страница 17: ...e and fail Use this callback to handle any request cleanup that must be executed regardless of the success state Note that if you do intend to use response data inside of this callback you must check...

Страница 18: ...S3 Amazon S3 List All of Your Buckets listBuckets The following example lists all buckets associated with your AWS account var s3 new AWS S3 s3 client listBuckets function err data for var index in d...

Страница 19: ...g var file require fs createWriteStream path to file jpg s3 client getObject params createReadStream pipe file Alternatively you can register an httpData event listener on the request object to access...

Отзывы: