我正在研究我的AWS证书,并且我试图找出以下js代码的工作原理:
var AWS = require('aws-sdk');
var uuid = require('node-uuid');
// Create an S3 client
var s3 = new AWS.S3();
// Create a bucket and upload something into it
var bucketName = 'node-sdk-sample-' + uuid.v4();
var keyName = 'hello_world.txt';
s3.createBucket({Bucket: bucketName}, function() {
var params = {Bucket: bucketName, Key: keyName, Body: 'Hello'};
s3.putObject(params, function(err, data) {
if (err)
console.log(err)
else
console.log("Successfully uploaded data to " + bucketName + "/" + keyName);
});
});
此代码成功加载包含单词" Hello"在里面。我不明白这个^如何识别我的AWS账户。它确实!但是怎么样!它能以某种方式确定我想在MY帐户中添加一个新存储桶,但此代码是直接从AWS文档中获取的。我不知道它怎么能搞清楚......
答案 0 :(得分:3)
根据Class: AWS.CredentialProviderChain,AWS SDK for JavaScript会在以下位置查找凭据:
AWS.CredentialProviderChain.defaultProviders = [
function () { return new AWS.EnvironmentCredentials('AWS'); },
function () { return new AWS.EnvironmentCredentials('AMAZON'); },
function () { return new AWS.SharedIniFileCredentials(); },
function () {
// if AWS_CONTAINER_CREDENTIALS_RELATIVE_URI is set
return new AWS.ECSCredentials();
// else
return new AWS.EC2MetadataCredentials();
}
]
强烈建议永远不要在应用程序中存储凭据。如果代码在Amazon EC2实例上运行且角色已分配给实例,则SDK将自动retrieve credentials from the instance metadata。
下一个最佳方法是将凭据存储在~/.aws/credentials
文件中。