env提供者 - 适用于 PHP 的 Amazon SDK
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

env提供者

使用环境变量来包含您的凭据可以防止您意外共享您的 Amazon 私有访问密钥。我们建议您不要在任何生产文件中将 Amazon 访问密钥直接添加到客户端。

要对 Amazon Web Services 进行身份验证,SDK 首先检查环境变量中的凭证。开发工具包会使用 getenv() 函数来查找 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKEN 环境变量。这些凭证称为环境凭证。有关如何获取这些值的说明,请参阅Amazon SDKs 和工具参考指南中的使用短期凭证进行身份验证

如果您在上托管应用程序 Amazon Elastic Beanstalk,则可以通过Amazon Elastic Beanstalk 控制台设置AWS_ACCESS_KEY_IDAWS_SECRET_KEY、和AWS_SESSION_TOKEN环境变量,这样 SDK 就可以自动使用这些凭证。

有关如何设置环境变量的更多信息,请参阅《工具参考指南》Amazon SDKs 和《工具参考指南》中的环境变量支持。另外,有关大多数支持的所有环境变量的列表 Amazon SDKs,请参阅环境变量列表

您也可以在命令行中设置环境变量,如下所示。

Linux

$ export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE # The access key for your Amazon Web Services 账户. $ export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY # The secret access key for your Amazon Web Services 账户. $ export AWS_SESSION_TOKEN=AQoDYXdzEJr...<remainder of security token> # The temporary session key for your Amazon Web Services 账户. # The AWS_SECURITY_TOKEN environment variable can also be used, but is only supported for backward compatibility purposes. # AWS_SESSION_TOKEN is supported by multiple Amazon SDKs other than PHP.

Windows

C:\> SET AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE # The access key for your Amazon Web Services 账户. C:\> SET AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY # The secret access key for your Amazon Web Services 账户. C:\> SET AWS_SESSION_TOKEN=AQoDYXdzEJr...<remainder of security token> # The temporary session key for your Amazon Web Services 账户. # The AWS_SECURITY_TOKEN environment variable can also be used, but is only supported for backward compatibility purposes. # AWS_SESSION_TOKEN is supported by multiple Amazon SDKs besides PHP.

Aws\Credentials\CredentialProvider::env 尝试从环境变量中加载凭证。

use Aws\Credentials\CredentialProvider; use Aws\S3\S3Client; $client = new S3Client([ 'region' => 'us-west-2', 'version' => '2006-03-01', 'credentials' => CredentialProvider::env() ]);