适用于 PHP 的 AWS 开发工具包:从实例配置文件元数据服务器检索凭证时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27400563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:21:05  来源:igfitidea点击:

AWS SDK for PHP: Error retrieving credentials from the instance profile metadata server

phpamazon-web-servicesgoogle-cloud-messagingaws-sdkamazon-sns

提问by Ravindra

I am trying to send SNS messeges to android through web api. Downloaded and installed the SDK from http://aws.amazon.com/developers/getting-started/php/

我正在尝试通过 web api 向 android 发送 SNS 消息。从http://aws.amazon.com/developers/getting-started/php/下载并安装了 SDK

Got following error while running sample.php:

运行 sample.php 时出现以下错误:

Fatal error: Uncaught exception 'Aws\Common\Exception\InstanceProfileCredentialsException' with message '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. ([curl] 28: Connection timed out after 5016 milliseconds [url] http://169.254.169.254/latest/meta-data/iam/security-credentials/)' in C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\InstanceMetadata\InstanceMetadataClient.php:85 Stack trace: #0 C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\Credentials\RefreshableInstanceProfileCredentials.php(52): Aws\Common\InstanceMetadata\InstanceMetadataClient->getInstanceProfileCredentials() #1 C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\Credentials\AbstractRefreshableCredentials.php(54): Aws\Common\Credentials\Refreshable in C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\InstanceMetadata\InstanceMetadataClient.php on line 85

A little guidance on this topic will help me a lot

关于这个主题的一些指导会对我有很大帮助

回答by shadi

In my case, I was using

就我而言,我正在使用

return DynamoDbClient::factory(array(
  'version' => 'latest',
  'region'  => AWS_REGION,
  'key' => AWS_KEY,
  'secret'  => AWS_SECRET
));

which used to be ok with aws/aws-sdk-phpversion 2.8.5 , but when composer automatically installed version 3.2.0, I got the error above. The problem is simply that I should've changed the way I made the call to

以前用aws/aws-sdk-php2.8.5 版没问题,但是当 composer 自动安装 3.2.0 版时,我得到了上面的错误。问题只是我应该改变我打电话的方式

return DynamoDbClient::factory(array(
  'version' => 'latest',
  'region'  => AWS_REGION,
  'credentials' => array(
    'key' => AWS_KEY,
    'secret'  => AWS_SECRET,
  )
));

as documented here. Without changing the call, the apachephp was falling back to looking for the ~/.aws/credentialsfile using the HOME environment variable, which was empty. You can check its value by running php -r 'var_dump(getenv("HOME"));'.

如此处所述。在不更改调用的情况下,apachephp 回退到~/.aws/credentials使用 HOME 环境变量查找文件,该变量为空。您可以通过运行来检查它的值php -r 'var_dump(getenv("HOME"));'

Thisis a related post

是一个相关的帖子

回答by elegant-user

In my case I had to use hard-coded credentials

就我而言,我不得不使用硬编码凭据

$s3Client = new S3Client([
    'region' => REGION,
    'version' => '2006-03-01',
    'credentials' => [
        'key'    => S3_KEY,
        'secret' => S3_SECRETE,
    ],
]);

See more details here:

在此处查看更多详细信息:

回答by Anti

You have to place the .aws/credentialsfile with your configuration in the home directory of the web service *usually /var/www) not in the home directory of the logged in user.

您必须将.aws/credentials带有配置的文件放在 Web 服务的主目录中*通常/var/www)而不是在登录用户的主目录中。

You can find what home directory you web service is using by running echo getenv('HOME');in a php file on your server.

通过echo getenv('HOME');在服务器上的 php 文件中运行,您可以找到 Web 服务使用的主目录。

回答by Edson Horacio Junior

I was trying to use a credentials file and got the same error, this guyon github pretty much nailed it:

我试图使用凭证文件并得到同样的错误,github 上的这个人几乎把它搞定了:

The credentials file should be in ini format but not have a .ini extension. It should have a 'default' section defined with your key and secret:

凭证文件应该是 ini 格式,但没有 .ini 扩展名。它应该有一个用您的密钥和秘密定义的“默认”部分:

$ less ~/.aws/credentials

[default]
aws_access_key_id = key
aws_secret_access_key = secret

If you specified other section name instead of default, just add a profilekey to the S3Client parameters:

如果您指定了其他部分名称而不是默认名称,只需profile向 S3Client 参数添加一个键:

[example]
aws_access_key_id = key
aws_secret_access_key = secret

$s3Client = new \Aws\S3\S3Client([
    'version' => '2006-03-01',
    'region' => $yourPreferredRegion,
    'profile' => 'example',
]);

Using a credentials file or environment variables is the recommended way of providing credentials on your own server

使用凭证文件或环境变量是在您自己的服务器上提供凭证的推荐方式

And @Anti 's answer also helped me alot!

@Anti 的回答也对我有很大帮助!

If you prefer the hard coded way, just follow @shadi 's answer.

如果您更喜欢硬编码方式,只需按照@shadi 的回答即可。

回答by Dibya Sahoo

Here are the steps:

以下是步骤:

  1. Type cd ~By this you will go into the home directory.
  2. mkdir .aws
  3. sudo vi .aws/credentials
  4. Write following lines and save the file.

    [default]
    aws_access_key_id = Your AWS Access Key
    
    aws_secret_access_key = Your AWS Secret Access Key
    
  1. 键入cd ~通过此您将进入主目录。
  2. mkdir .aws
  3. sudo vi .aws/credentials
  4. 编写以下几行并保存文件。

    [default]
    aws_access_key_id = Your AWS Access Key
    
    aws_secret_access_key = Your AWS Secret Access Key
    

回答by JenuJ

If it is laravel and aws/aws-sdk-php-laravel sdk then after configuring all step and defining key in .env file you have to drop config cache and rebuild it by following commands.

如果它是 laravel 和 aws/aws-sdk-php-laravel sdk,那么在配置所有步骤并在 .env 文件中定义密钥后,您必须删除配置缓存并通过以下命令重建它。

php artisan config:cache;
composer dump-autoload;

回答by MichaelHoughton

This might be because the config file hasn't been published.

这可能是因为配置文件尚未发布。

Be sure to publish the config file:

确保发布配置文件:

php artisan vendor:publish  --provider="Aws\Laravel\AwsServiceProvider"

To test this is the issue, just clear the config.

要测试这是问题所在,只需清除配置即可。

php artisan config:clear

If it works with the cache cleared, then this will be the issue.

如果它在清除缓存的情况下工作,那么这将是问题。

回答by Narayan

You can try these lines:

您可以尝试以下几行:

$credentials = new Aws\Credentials\Credentials('key' , 'secret-key');

$credentials = new Aws\Credentials\Credentials('key', 'secret-key');

$s3 = new S3Client(['version' => 'latest','region' => 'ap-south-1','credentials'=>$credentials]);

$s3 = new S3Client(['version' => 'latest','region' => 'ap-south-1','credentials'=>$credentials]);