/** * Get a CloudFront URL for an S3 key. * * @param $key * The S3 object key. * @param int $expiry * (optional) Expiry time for the URL, as a Unix timestamp. * @return \Guzzle\Http\Url * The CloudFront URL. */ protected function getCloudFrontUrl($key, $expiry = NULL) { // Real CloudFront credentials are required to test this, so we ignore // testing this. // @codeCoverageIgnoreStart $cf = $this->config->getCloudFront(); $url = new Url('https', $this->config->getDomain()); $url->setPath($key); $this->injectCname($url); $options = array('url' => (string) $url, 'expires' => $expiry); $url = Url::factory($cf->getSignedUrl($options)); return $url; // @codeCoverageIgnoreEnd }
/** * Overrides factory() to support bucket configs. * * @param string $url * Full URL used to create a Url object. * @param \Drupal\amazons3\StreamWrapperConfiguration $config * (optional) Configuration to associate with this URL. * * @throws \InvalidArgumentException * Thrown when $url cannot be parsed by parse_url(). * * @return static * An S3Url. */ public static function factory($url, StreamWrapperConfiguration $config = null) { !$config ? $bucket = null : ($bucket = $config->getBucket()); $defaults = array('scheme' => 's3', 'host' => $bucket, 'path' => null, 'port' => null, 'query' => null, 'user' => null, 'pass' => null, 'fragment' => null); if (false === ($parts = parse_url($url))) { throw new \InvalidArgumentException('Was unable to parse malformed url: ' . $url); } $parts += $defaults; return new static($parts['host'], substr($parts['path'], 1)); }
/** * Construct a new stream wrapper. * * @param \Drupal\amazons3\StreamWrapperConfiguration $config * (optional) A specific configuration to use for this wrapper. */ public function __construct(StreamWrapperConfiguration $config = NULL) { if (!$config) { if (static::$defaultConfig) { $config = static::$defaultConfig; } else { // @codeCoverageIgnoreStart $config = StreamWrapperConfiguration::fromDrupalVariables(); // @codeCoverageIgnoreEnd } } $this->config = $config; if (!$this->getClient()) { /** @var S3Client $name */ $name = static::$s3ClientClass; $this->setClient($name::factory()); } if ($this->config->isCaching() && !static::$cache) { static::attachCache(new DoctrineCacheAdapter(new ChainCache([new ArrayCache(), new Cache()])), $this->config->getCacheLifetime()); } }