Pav*_*van 0 php apache amazon-web-services laravel 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 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/)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么当我aws.php在 Laravel 设置的 config 文件夹中出现此错误时。这里存储访问密钥和秘密密钥。
我不太喜欢这个,因为这会导致我的凭据存储在其他人可以通过 SFTP 访问的文件夹设置中。
我还拥有服务器根文件夹中的 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));
}
Run Code Online (Sandbox Code Playgroud)
请注意,我使用的default配置文件应从存储配置和凭证文件的 aws 根目录获取凭证。
现在,作为临时解决方案,我这样做了:http ://blog.ianholden.com/aws-s3-with-php-on-apache/这使得一切正常,但我不高兴让我的凭证密钥存在于应用程序代码。感觉不对。
有人可以指导我解决此错误吗?
更新1:评论:~/.aws/下的config内容是什么~/.aws/这是文件夹
中config文件的内容
[default]
output = json
region = us-west-2
Run Code Online (Sandbox Code Playgroud)
您的~/.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
Run Code Online (Sandbox Code Playgroud)
库使用以下函数来确定在其中查找文件的目录.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;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,如果未设置HOME或HOMEDRIVE和HOMEPATH环境变量,则库将无法找到您的凭据文件。
除了能够找到该文件之外,它还需要可由 PHP 读取,并且采用 INI 格式(如上所述)。
从评论看来,HOME环境变量没有设置。因此,图书馆无法找到您的凭据文件。从这里到哪里有几个选择。
选项1:
修复您的服务器,以便正确设置 HOME 环境变量。您需要对执行此操作的最佳方法进行一些研究,因为该解决方案可能高度依赖于服务器/apache/php 配置...
选项2:
由于您使用的是 Laravel 5,因此您可以将凭据添加到.env文件中,然后在代码中访问它们。例如,将以下两行添加到文件末尾.env:
AWS_ACCESS_KEY_ID=YOUR_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY=YOUR_AWS_SECRET_ACCESS_KEY
Run Code Online (Sandbox Code Playgroud)
现在,在您的代码中,您可以执行以下操作:
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
));
}
Run Code Online (Sandbox Code Playgroud)
选项 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
));
}
Run Code Online (Sandbox Code Playgroud)