developer tip

PHP 용 AWS SDK : 인스턴스 프로필 메타 데이터 서버에서 자격 증명을 검색하는 동안 오류가 발생했습니다.

optionbox 2021. 1. 5. 07:58
반응형

PHP 용 AWS SDK : 인스턴스 프로필 메타 데이터 서버에서 자격 증명을 검색하는 동안 오류가 발생했습니다.


웹 API를 통해 Android에 SNS 메시지를 보내려고합니다. http://aws.amazon.com/developers/getting-started/php/ 에서 SDK를 다운로드하고 설치했습니다.

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

이 주제에 대한 약간의 안내가 도움이 될 것입니다.


제 경우에는

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

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,
  )
));

여기에 설명 된대로 . 호출을 변경하지 않고 apache php ~/.aws/credentials는 비어있는 HOME 환경 변수를 사용하여 파일 을 찾는 것으로 되돌아갔습니다 . 을 실행하여 값을 확인할 수 있습니다 php -r 'var_dump(getenv("HOME"));'.

이것은 관련 게시물입니다


제 경우에는 하드 코딩 된 자격 증명 을 사용해야했습니다.

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

여기에서 자세한 내용을 참조 하십시오 .


로그인 한 사용자의 홈 디렉토리가 아닌 .aws/credentials웹 서비스의 홈 디렉토리 * 일반적으로 /var/www)에 구성 파일 을 배치해야 합니다 .

echo getenv('HOME');서버의 php 파일에서 실행하여 웹 서비스가 사용중인 홈 디렉토리를 찾을 수 있습니다 .


단계는 다음과 같습니다.

  1. cd ~By this를 입력 하면 홈 디렉토리로 이동합니다.
  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
    

자격 증명 파일을 사용하려고했는데 똑같은 오류가 발생 했습니다. github 의이 사람이 거의 문제를 해결했습니다.

자격 증명 파일은 ini 형식이어야하지만 .ini 확장자가 없어야합니다. 키와 비밀로 정의 된 '기본'섹션이 있어야합니다.

$ less ~/.aws/credentials

[default]
aws_access_key_id = key
aws_secret_access_key = secret

기본값 대신 다른 섹션 이름을 지정한 경우 profileS3Client 매개 변수에 키를 추가하기 만하면됩니다.

[example]
aws_access_key_id = key
aws_secret_access_key = secret

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

자격 증명 파일 또는 환경 변수를 사용하는 것이 자체 서버에서 자격 증명을 제공하는 데 권장되는 방법입니다.

그리고 @Anti의 답변도 저를 많이 도왔습니다!

하드 코드 방식을 선호하는 경우 @shadi의 답변을 따르십시오.


구성 파일이 게시되지 않았기 때문일 수 있습니다.

구성 파일을 게시해야합니다.

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

이것이 문제인지 테스트하려면 구성을 지우십시오.

php artisan config:clear

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


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.

php artisan config:cache;
composer dump-autoload;

You can try these lines:

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

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

ReferenceURL : https://stackoverflow.com/questions/27400563/aws-sdk-for-php-error-retrieving-credentials-from-the-instance-profile-metadata

반응형