如何将访问密钥放在AWS SDK for PHP中?

时间:2018-10-13 23:20:30

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

我正在尝试将aws转换为使用wasabi api上传,并且它说它与aws一起运行,因此我希望在实例化后放入访问密钥,但找不到该密钥放入的文件。另一方面,如果您知道如何将wasabi 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'];
    }
}

1 个答案:

答案 0 :(得分:0)

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

(1, 1, 1) or (0, 0, 0)

它将设置存储在aws configure 中。

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