【问题标题】:Where do I put the access key in AWS SDK for PHP?我应该将访问密钥放在 AWS SDK for PHP 中的什么位置?
【发布时间】:2019-03-18 18:51:35
【问题描述】:

我正在尝试将 aws 转换为使用芥末 api 上传,它说它使用 aws 运行,所以我希望在实例化后放入我的访问密钥,但找不到密钥所在的文件。另一方面,如果您知道如何将芥末 s3 api 集成到我的网站中,那将对我有很大的帮助,谢谢。

aws/credientials/credientials.php

<?php
namespace Aws\Credentials;

/**
 * Basic implementation of the AWS Credentials interface that allows callers to
 * pass in the AWS Access Key and AWS Secret Access Key in the constructor.
 */
class Credentials implements CredentialsInterface, \Serializable
{
    private $key;
    private $secret;
    private $token;
    private $expires;

    /**
     * Constructs a new BasicAWSCredentials object, with the specified AWS
     * access key and AWS secret key
     *
     * @param string $key     AWS access key ID
     * @param string $secret  AWS secret access key
     * @param string $token   Security token to use
     * @param int    $expires UNIX timestamp for when credentials expire
     */
    public function __construct($key, $secret, $token = null, $expires = null)
    {
        $this->key = trim($key);
        $this->secret = trim($secret);
        $this->token = $token;
        $this->expires = $expires;
    }

    public static function __set_state(array $state)
    {
        return new self(
            $state['key'],
            $state['secret'],
            $state['token'],
            $state['expires']
        );
    }

    public function getAccessKeyId()
    {
        return $this->key;
    }

    public function getSecretKey()
    {
        return $this->secret;
    }

    public function getSecurityToken()
    {
        return $this->token;
    }

    public function getExpiration()
    {
        return $this->expires;
    }

    public function isExpired()
    {
        return $this->expires !== null && time() >= $this->expires;
    }

    public function toArray()
    {
        return [
            'key'     => $this->key,
            'secret'  => $this->secret,
            'token'   => $this->token,
            'expires' => $this->expires
        ];
    }

    public function serialize()
    {
        return json_encode($this->toArray());
    }

    public function unserialize($serialized)
    {
        $data = json_decode($serialized, true);

        $this->key = $data['key'];
        $this->secret = $data['secret'];
        $this->token = $data['token'];
        $this->expires = $data['expires'];
    }
}

【问题讨论】:

    标签: php angularjs amazon-web-services sdk wasabi


    【解决方案1】:

    如果你在某处使用AWS Command-Line Interface (CLI),那么最简单的方法是运行:

    aws configure
    

    它将设置存储在~/.aws/credentials中。

    然后,您的代码不需要引用凭据。 SDK 将自动从该文件中检索凭据。

    【讨论】:

    • 但我正在尝试使用 wasabi s3 api 输入新凭据?
    【解决方案2】:
    require('vendor/autoload.php');
    
    use Aws\Ec2\Ec2Client;
    

    $credentials = new Aws\Credentials\Credentials('您的访问密钥', '你的密钥'); // 两个键都放在这里

    $ec2Client = new Aws\Ec2\Ec2Client([
        'version' => 'latest',
        'region'  => 'ap-south-1',
        'credentials' => $credentials
    ]);
    
    $result = $ec2Client->describeKeyPairs();
    
    echo '<pre>';
    print_r($result);
    

    参考网站:https://docs.aws.amazon.com/aws-sdk-php/v2/guide/credentials.html#passing-credentials-into-a-client-factory-method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-17
      • 2012-07-22
      • 2020-04-10
      • 1970-01-01
      • 2021-09-14
      • 2014-12-05
      • 1970-01-01
      • 2013-04-17
      相关资源
      最近更新 更多