【问题标题】:SDK Error while trying to add caching to KMS尝试向 KMS 添加缓存时出现 SDK 错误
【发布时间】:2015-05-14 15:51:39
【问题描述】:

我收到以下错误

Fatal error: Uncaught exception 'Aws\Common\Exception\InvalidArgumentException' with message 'Unable to utilize caching with the specified options' in vendor/aws/aws-sdk-php/src/Aws/Common/Credentials/Credentials.php:305 Stack trace: 

#0 vendor/aws/aws-sdk-php/src/Aws/Common/Credentials/Credentials.php(113): Aws\Common\Credentials\Credentials::createCache(Object(Aws\Common\Credentials\RefreshableInstanceProfileCredentials), Object(Illuminate\Cache\ApcStore), 'credentials_214...') 

#1 vendor/aws/aws-sdk-php/src/Aws/Common/Client/ClientBuilder.php(431): Aws\Common\Credentials\Credentials::factory(Object(Guzzle\Common\Collection)) 

#2 vendor/aws/aws-sdk-php/src/Aws/Common/Client/ClientBuilder.php(227): Aws\Common\Client\ClientBuilder->getCredent in vendor/aws/aws-sdk-php/src/Aws/Common/Credentials/Credentials.php on line 305

使用以下代码

use Aws\Kms\Exception\KmsException,
    Aws\Kms\KmsClient;

....

public function __construct($region = 'us-east-1')
{
    $this->cacheAdapter = New Illuminate\Cache\ApcStore(New Illuminate\Cache\ApcWrapper());

    $this->region = $region;
    $this->kms = KmsClient::factory([
        'credentials.cache' => $this->cacheAdapter,
        'region'            => $this->region,
    ]);
}

我认为这需要实现Guzzle CacheAdaperInterface,但不确定该怎么做?

【问题讨论】:

    标签: php amazon-web-services interface


    【解决方案1】:

    我在解决问题的 SDK 前使用了缓存服务。

    <?php
    
    Namespace Library\Cache\Aws
    {
      USE Guzzle\Cache\CacheAdapterInterface as CacheInterface;
    
      Class ApcAdapter Implements CacheInterface
      {
        /**
         * Test if an entry exists in the cache.
         *
         * @param string $id cache id The cache id of the entry to check for.
         * @param array $options Array of cache adapter options
         * @return bool Returns TRUE if a cache entry exists for the given cache id, FALSE otherwise.
         */
        public function contains($id, array $options = null)
        {
            return apc_exists($id);
        }
    
        /**
         * Deletes a cache entry.
         *
         * @param string $id cache id
         * @param array $options Array of cache adapter options
         * @return bool TRUE on success, FALSE on failure
         */
        public function delete($id, array $options = null)
        {
            return apc_delete($id);
        }
    
        /**
         * Fetches an entry from the cache.
         *
         * @param string $id cache id The id of the cache entry to fetch.
         * @param array $options Array of cache adapter options
         * @return string The cached data or FALSE, if no cache entry exists for the given id.
         */
        public function fetch($id, array $options = null)
        {
            return apc_fetch($id);
        }
    
        /**
         * Puts data into the cache.
         *
         * @param string $id The cache id
         * @param string $data The cache entry/data
         * @param int|bool $lifeTime The lifetime. If != false, sets a specific lifetime for this cache entry
         * @param array $options Array of cache adapter options
         * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
         */
        public function save($id, $data, $lifeTime = false, array $options = null)
        {
            return apc_add($id, $data, intval($lifeTime));
        }
      }
    }
    
    Namespace Library\ServiceBuilder
    {
    
      USE Aws\Common\Aws               AS ServiceBuilder,
          Aws\DynamoDb\DynamoDbClient  AS DynamoDbClient,
          Aws\Kms\KmsClient            AS KmsClient,
          Library\Cache\Aws\ApcAdapter AS ApcAdapter;
    
      Class Aws
      {
        protected static $_instance = [];
        protected        $_awsInstance;
        protected        $_region;
    
        protected function __construct($region)
        {
            $cacheAdapter = New ApcAdapter();
            $this->_region = $region;
            $this->_awsInstance = ServiceBuilder::factory(null, [
                  'credentials.cache' => $cacheAdapter, 
                  'region'            => $region,
                ]
            );
    
        }
    
        /**
         * @param string $region [opt, default=us-east-1]
         * @static
         * @return \Engrade\ServiceBuilder\Aws
         */
        public static function getInstance($region = 'us-east-1')
        {
            if (! array_key_exists($region, self::$_instance)) 
            {
                self::$_instance[$region] = New self($region);
            }
    
            return self::$_instance[$region];
        }
    
        /**
         * @return KmsClient
         */
        public function getKmsClient() 
        {
            return $this->_awsInstance->get('kms');
        }
    
        /**
         * @return DynamoDbClient
         */
        public function getDynamoDbClient() 
        {
            return $this->_awsInstance->get('dynamodb');
        }
      }
    }
    
    Namespace
    {
      USE Library\ServiceBuilder AS ServiceBuilder
          Aws\Kms\Exception\KmsException,
          Aws\Kms\KmsClient;
    
      ....
    
      public function __construct($region = 'us-east-1')
      {
        $this->region = $region;
    
        $aws = ServiceBuilder\Aws::getInstance();
        $kms = $aws->getKmsClient();
    
        $this->kms = $kms::factory([
          'region' => $this->region,
        ]);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-21
      • 2013-10-18
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多