阅读背景:

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

来源:互联网 

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

我试图通过web api将SNS messeges发送到android。从https://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] https://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

关于这个主题的一点指导将帮助我很多

5 个解决方案

#1


124  

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-php version 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-php版本2.8.5,但是当作曲家自动安装版本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 apache php was falling back to looking for the ~/.aws/credentials file using the HOME environment variable, which was empty. You can check its value by running php -r 'var_dump(getenv("HOME"));'.

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

This is a related post

这是一篇相关的帖子

#2


7  

You have to place the .aws/credentials file 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')找到您的Web服务正在使用的主目录;在您的服务器上的PHP文件中。

#3


4  

Here are the steps:

以下是步骤:

  1. Type cd ~ By this you will go into the home directory.
  2. 键入cd~这样您将进入主目录。
  3. mkdir .aws
  4. mkdir .aws
  5. sudo vi .aws/credentials
  6. sudo vi .aws / credentials
  7. 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
    

#4


3  

I was trying to use a credentials file and got the same error, this guy on 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 profile key to the S3Client parameters:

如果您指定了其他节名而不是默认值,只需将配置文件键添加到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的回答。

#5


3  

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:

在这里查看更多细节:


分享到: