AWS环境变量提供程序不接受凭据

时间:2017-10-22 03:14:04

标签: amazon-web-services amazon-dynamodb dynamo-local

我使用以下代码在本地连接dynamoDb

$dotenv = new Dotenv\Dotenv(dirname(__DIR__, 2));
    $dotenv->load();
    $this->key = getenv('aws_access_key_id');
    $this->secret = getenv('aws_secret_access_key');
    $this->dynamodb = new DynamoDbClient([
        'profile' => 'default',
        'region' => 'us-east-1',
        'version' => 'latest',
        'credentials' => [
            'key' => $this->key,
            'secret' => $this->secret,
        ]
    ]);

我的.env文件

aws_access_key_id = ....
aws_secret_access_key = ....

我收到此错误

Type: Aws\Exception\CredentialsException
Message: Cannot read credentials from /Users/user/.aws/credentials
File: /path/to/project/vendor/aws/aws-sdk-php/src/Credentials/CredentialProvider.php
Line: 394

尝试access key&的基本硬编码secret key也是如此,但每次从默认路径~/.aws/credentials

获取时,它似乎都不起作用

TIA

2 个答案:

答案 0 :(得分:2)

尝试不指定:

'profile' => 'default'

您想使用凭证文件中指定的默认配置文件。 SDK可能会查看此行并尝试打开凭据文件并查找default个人资料。我没有安装PHP SDK来验证这一点..

答案 1 :(得分:0)

尝试打印环境变量以确保您的应用程序获取这些环境变量。

没有从这些变量中找到它。您需要在运行此脚本之前导出它们。

希望它有所帮助。

<强> EDIT1:

var inputStrings = new List<string>
{
    "one",
    "two words",
    "this is three",
    "this one is four",
    "and this one has five"
};

foreach(var input in inputStrings)
{
    var result = Convert.ToString((input.Split(' ').Length + 1), 2).PadLeft(8, '0');

    Console.WriteLine($"{input.PadRight(22, ' ')} = {result}");
}

Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();

//将这些凭据分配给Dynamodbclient

EDIT2: AWS SDK 3.x遵循不同的初始化方式。

use Aws\Common\Credentials\Credentials;

$credentials = new Credentials('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');