Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AWS PHP SDK凭据错误S_Php_Apache_Laravel_Amazon Web Services_Aws Php Sdk - Fatal编程技术网

AWS PHP SDK凭据错误S

AWS PHP SDK凭据错误S,php,apache,laravel,amazon-web-services,aws-php-sdk,Php,Apache,Laravel,Amazon Web Services,Aws Php Sdk,我在尝试创建SNSClient对象时遇到以下错误 InstanceProfileCredentialsException in InstanceMetadataClient.php line 85: Error retrieving credentials from the instance profile metadata server. When you are not running inside of Amazon EC2, you must provide your AWS acces

我在尝试创建
SNSClient
对象时遇到以下错误

InstanceProfileCredentialsException in InstanceMetadataClient.php line 85: Error retrieving credentials from the instance profile metadata server. When you are not running inside of Amazon EC2, you must provide your AWS access key ID and secret access key in the "key" and "secret" options when creating a client or provide an instantiated Aws\Common\Credentials\CredentialsInterface object. (Client error response
[status code] 404
[reason phrase] Not Found
[url] http://169.254.169.254/latest/meta-data/iam/security-credentials/)
当我在laravel设置的config文件夹中有我的
aws.php
时,我不明白为什么会出现这个错误。这里存储了访问密钥和密钥

我真的不喜欢这样,因为这会导致我的凭据存储在文件夹设置中,其他人可能会将其放入其中

我还有服务器根文件夹中的aws凭据和配置文件,路径如下:
~/.aws/
,但由于某些原因,应用程序不满意

我的代码是这样的:

public function about(){

    // Instantiate a client with the credentials from the project1 profile
    $snsClient = SnsClient::factory(array(
            'profile' => 'default',
            'region'  => 'us-west-2',
        ));

    // Get the application's endpoints

    $iOS_AppArn = "arn:aws:sns:us-west-2:235418406768:app/APNS_SANDBOX/ClashTarget";

    $iOS_model = $snsClient->listEndpointsByPlatformApplication(array('PlatformApplicationArn' => $iOS_AppArn));
}
注意,我使用的是
default
配置文件,它应该从存储配置文件和凭证文件的根aws目录获取凭证

现在,作为一个临时解决方案,我这样做了:这使一切都可以工作,但我不高兴我的凭证密钥存在于应用程序代码中。感觉不对

有人能指导我解决这个错误吗

更新1:注释~/.aws/ 这是
~/.aws/
文件夹中配置文件的内容

[default]
output = json
region = us-west-2

我看不出你出错的确切原因,但是

我真的不喜欢这样,因为这会导致存储我的凭据 在文件夹设置中,其他人可能会进入该文件夹


我想您肯定需要在AWS控制台上设置IAM角色,以允许从EC2实例连接到SNS。这样,您就不需要提供任何凭据了

我看不出出现错误的确切原因,但是

我真的不喜欢这样,因为这会导致存储我的凭据 在文件夹设置中,其他人可能会进入该文件夹


我想您肯定需要在AWS控制台上设置IAM角色,以允许从EC2实例连接到SNS。这样,您就不需要提供任何凭证

您的
~/。aws/凭证
文件应如下所示:

[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

[project1]
aws_access_key_id = ANOTHER_AWS_ACCESS_KEY_ID
aws_secret_access_key = ANOTHER_AWS_SECRET_ACCESS_KEY
库使用以下函数确定要在其中查找
.aws/credentials
文件的目录:

private static function getHomeDir()
{
    // On Linux/Unix-like systems, use the HOME environment variable
    if ($homeDir = getenv('HOME')) {
        return $homeDir;
    }
    // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
    $homeDrive = getenv('HOMEDRIVE');
    $homePath = getenv('HOMEPATH');
    return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
}
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
如您所见,如果未设置
HOME
HOMEDRIVE
HOMEPATH
环境变量,库将无法找到您的凭据文件

除了能够找到文件外,它还需要能够被PHP读取,并且是INI格式(如上所述)

编辑 从注释中,听起来好像没有设置
HOME
环境变量。因此,库无法找到您的凭据文件。有几个选项可以选择从这里走到哪里

选项1:

修复服务器,以便正确设置主环境变量。您需要对实现这一点的最佳方法进行一些研究,因为解决方案可能高度依赖于服务器/apache/php配置

选项2:

由于您使用的是Laravel 5,因此可以将凭据添加到
.env
文件中,然后在代码中访问它们。例如,在
.env
文件末尾添加以下两行:

private static function getHomeDir()
{
    // On Linux/Unix-like systems, use the HOME environment variable
    if ($homeDir = getenv('HOME')) {
        return $homeDir;
    }
    // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
    $homeDrive = getenv('HOMEDRIVE');
    $homePath = getenv('HOMEPATH');
    return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
}
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
现在,在代码中,您可以执行以下操作:

public function about() {
    // add "use Aws\Credentials\Credentials;" at the top of your file

    // the env() method reads the values from the .env file. ideally you should never
    // use the env() method outside of a config file, though, so I'd suggest adding
    // a new config file that uses the env statements and using the config here.
    $credentials = new Credentials(env('AWS_ACCESS_KEY_ID'), env('AWS_SECRET_ACCESS_KEY'));

    // if using the 'credentials' key, do not use the 'profile' key
    $snsClient = SnsClient::factory(array(
        'region'  => 'us-west-2',
        'credentials' => $credentials
    ));
}
选项3:

自己调用
CredentialProvider::ini()
方法,并将配置文件和完整路径传递给ini文件。这将绕过库尝试确定主目录的需要

public function about() {
    // add "use Aws\Credentials\CredentialProvider;" at the top of your file

    // the first parameter is the profile, the second parameter is the full path to
    // your credentials file. You may want to add this to your .env file, and
    // access using env()/config() instead of hardcoding here, though.
    $credentials = CredentialProvider::ini('default', '/root/.aws/credentials');

    // if using the 'credentials' key, do not use the 'profile' key
    $snsClient = SnsClient::factory(array(
        'region'  => 'us-west-2',
        'credentials' => $credentials
    ));
}

您的
~/.aws/credentials
文件应如下所示:

[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

[project1]
aws_access_key_id = ANOTHER_AWS_ACCESS_KEY_ID
aws_secret_access_key = ANOTHER_AWS_SECRET_ACCESS_KEY
库使用以下函数确定要在其中查找
.aws/credentials
文件的目录:

private static function getHomeDir()
{
    // On Linux/Unix-like systems, use the HOME environment variable
    if ($homeDir = getenv('HOME')) {
        return $homeDir;
    }
    // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
    $homeDrive = getenv('HOMEDRIVE');
    $homePath = getenv('HOMEPATH');
    return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
}
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
如您所见,如果未设置
HOME
HOMEDRIVE
HOMEPATH
环境变量,库将无法找到您的凭据文件

除了能够找到文件外,它还需要能够被PHP读取,并且是INI格式(如上所述)

编辑 从注释中,听起来好像没有设置
HOME
环境变量。因此,库无法找到您的凭据文件。有几个选项可以选择从这里走到哪里

选项1:

修复服务器,以便正确设置主环境变量。您需要对实现这一点的最佳方法进行一些研究,因为解决方案可能高度依赖于服务器/apache/php配置

选项2:

由于您使用的是Laravel 5,因此可以将凭据添加到
.env
文件中,然后在代码中访问它们。例如,在
.env
文件末尾添加以下两行:

private static function getHomeDir()
{
    // On Linux/Unix-like systems, use the HOME environment variable
    if ($homeDir = getenv('HOME')) {
        return $homeDir;
    }
    // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
    $homeDrive = getenv('HOMEDRIVE');
    $homePath = getenv('HOMEPATH');
    return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
}
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
现在,在代码中,您可以执行以下操作:

public function about() {
    // add "use Aws\Credentials\Credentials;" at the top of your file

    // the env() method reads the values from the .env file. ideally you should never
    // use the env() method outside of a config file, though, so I'd suggest adding
    // a new config file that uses the env statements and using the config here.
    $credentials = new Credentials(env('AWS_ACCESS_KEY_ID'), env('AWS_SECRET_ACCESS_KEY'));

    // if using the 'credentials' key, do not use the 'profile' key
    $snsClient = SnsClient::factory(array(
        'region'  => 'us-west-2',
        'credentials' => $credentials
    ));
}
选项3:

自己调用
CredentialProvider::ini()
方法,并将配置文件和完整路径传递给ini文件。这将绕过库尝试确定主目录的需要

public function about() {
    // add "use Aws\Credentials\CredentialProvider;" at the top of your file

    // the first parameter is the profile, the second parameter is the full path to
    // your credentials file. You may want to add this to your .env file, and
    // access using env()/config() instead of hardcoding here, though.
    $credentials = CredentialProvider::ini('default', '/root/.aws/credentials');

    // if using the 'credentials' key, do not use the 'profile' key
    $snsClient = SnsClient::factory(array(
        'region'  => 'us-west-2',
        'credentials' => $credentials
    ));
}

从@Pavan对其问题的评论中,他在
~/.aws
中显示了凭证文件的内容,似乎有两个文件。我只有一个文件

$ cat ~/.aws/credentials
[default]
aws_access_key_id = blablabla
aws_secret_access_key = blablabla
region = us-west-2

从@Pavan对其问题的评论中,他在
~/.aws
中显示了凭证文件的内容,似乎有两个文件。我只有一个文件

$ cat ~/.aws/credentials
[default]
aws_access_key_id = blablabla
aws_secret_access_key = blablabla
region = us-west-2

请在~/.aws/What do
print_r(getenv('HOME'))显示配置的内容
$func=\Aws\Credentials\CredentialProvider::ini();打印($func())显示?@EvgeniyKuzmin请查看我帖子中的更新1。如果您还想知道同一文件夹中凭证文件的内容,这里是:
aws_access_key_id=AKIAKDH4JFYI3IRK7SAQ
第1行和
aws_secret_access_key=2C3p0HdIZ2/5vfgzgz1e8qqbennskxmz2vcolodo
第2行显然有一些字符更改。@patricus I am g