适用于 PHP 的 SDK 中的 process 提供程序
Aws\Credentials\CredentialProvider::process 尝试通过执行共享 Amazon 配置文件中的配置文件中指定的 credential_process 值来加载凭证。
默认情况下,SDK 会首先尝试从位于 ~/.aws/credentials 的共享 Amazon credentials 文件中加载“默认”配置文件。如果在共享 credentials 文件中找不到“默认”配置文件,SDK 将在共享 config 文件中查找默认配置文件。下面是共享 credentials 文件的配置示例。
[default] credential_process = /path/to/file/credential_returning_executable.sh --custom-command custom_parameter
SDK 将通过使用 PHP 的 shell_exec 函数完全调用给定的 credential_process 命令,然后从 stdout 中读取 JSON 数据。credential_process 必须采用以下格式将凭证写入 stdout:
{ "Version": 1, "AccessKeyId": "", "SecretAccessKey": "", "SessionToken": "", "Expiration": "" }
SessionToken 和 Expiration 是可选的。如果存在,凭证将被视为临时的。
use Aws\Credentials\CredentialProvider; use Aws\S3\S3Client; $provider = CredentialProvider::process(); // Cache the results in a memoize function to avoid loading and parsing // the ini file on every API operation $provider = CredentialProvider::memoize($provider); $client = new S3Client([ 'region' => 'us-west-2', 'version' => '2006-03-01', 'credentials' => $provider ]);
您可以通过向创建提供程序的函数提供参数来使用自定义配置文件或 .ini 文件位置。
$profile = 'production'; $path = '/full/path/to/credentials.ini'; $provider = CredentialProvider::process($profile, $path); $provider = CredentialProvider::memoize($provider); $client = new S3Client([ 'region' => 'us-west-2', 'version' => '2006-03-01', 'credentials' => $provider ]);