AWS資格クラスは、環境変数「HOME」を持っているかのように思えます。資格ファイルへのパスを解決し、それに応じて 'HOME'環境変数を設定するのに役立つ関数がいくつかあります。
/**
* determine path to credentials file
* @return string
* */
protected function _getCredentialsPath(){
// https://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/credentials.html#credential-profiles
$credentials_path = '';
$current_user = get_current_user();
$credentials_file = '.aws/credentials';
$possible_paths = array('/Users/%s/%s', '/home/%s/%s');
forEach($possible_paths AS $check_path){
$check_credentials_path = sprintf($check_path, $current_user, $credentials_file);
if(file_exists($check_credentials_path)){
$credentials_path = realpath($check_credentials_path);
break;
}
}
// set the HOME environment variable if missing
if(empty(getenv('HOME')) && ! empty($credentials_path)){
$directory = pathinfo($credentials_path, PATHINFO_DIRNAME);
$exploded_path = explode(DIRECTORY_SEPARATOR, $directory);
array_pop($exploded_path); // remove .aws directory from end
$home_path = implode(DIRECTORY_SEPARATOR, $exploded_path);
putenv("HOME={$home_path}");
}
return $credentials_path;
}
/**
* get the aws sqs client
* @return \Aws\Sqs\SqsClient
* */
protected function _getAwsSqsClient(){
if(empty($this->_aws_sqs_client)){
// SDK Version 3 Syntax
$credential_profile = $this->_aws_sqs_profile;
$credential_path = $this->_getCredentialsPath();
$credential_provider = CredentialProvider::ini($credential_profile, $credential_path);
$credential_provider = CredentialProvider::memoize($credential_provider);
// http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html
$sdk = new \Aws\Sdk(
array(
'region' => $this->_aws_region,
'version' => $this->_aws_sdk_version,
'profile' => $this->_aws_sqs_profile,
'credentials' => $credential_provider,
)
);
$sqs_client = $sdk->createSqs();
$this->_aws_sqs_client = $sqs_client;
try{
$response = $sqs_client->getQueueUrl(array('QueueName' => self::AMAZON_SQS_QUEUE_NAME));
}
catch(\Exception $e){
echo "home: ",getenv('HOME'),"<br />";
echo "credential profile: ",$credential_profile,"<br />";
echo "credential path: ",$credential_path,"<br />";
echo "<pre style='color:purple'>credential provider: "; var_dump($credential_provider); echo "</pre>\n<br />";
$file_handle = fopen($credential_path, 'r');
echo "credential content: <pre style='color:blue;'>",fread($file_handle, filesize($credential_path)),"</pre>\n<br />";
echo "<pre style='color:red;'>"; var_dump($e->getMessage()); echo "</pre>\n";
die(__FILE__.__LINE__);
}
$this->_aws_queue_url = $response->QueueUrl;
}
return $this->_aws_sqs_client;
}
ファイルのパーミッションやapacheのユーザーが変わったのか? –
@ sef4eg私は 'credentials'ファイルのアクセス権やapacheユーザを変更しませんでしたが、私は今日働く可能性を試してみました。 – wogsland