Skip to content

Commit

Permalink
Merge pull request #3 from NiR-/fix/resolve-aws-sdk-v2
Browse files Browse the repository at this point in the history
Add support for aws-sdk-php v2
  • Loading branch information
Albin Kerouanton authored Jun 2, 2017
2 parents 6346ace + 16a3b63 commit 6a0c0e2
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 16 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ php:

before_script:
- composer self-update && composer install --prefer-source --no-interaction
- if [[ ${TRAVIS_PHP_VERSION:0:1} == "5" ]]; then composer update --prefer-lowest && composer update knplabs/gaufrette; fi

script:
- php bin/phpspec run -fpretty --verbose
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
},
"require-dev": {
"phpspec/phpspec": "^3.4",
"phpunit/phpunit": "^5",
"aws/aws-sdk-php": "~3"
"phpunit/phpunit": "~5.5",
"aws/aws-sdk-php": "~2.4|~3"
},
"license": "MIT",
"authors": [
Expand Down
12 changes: 0 additions & 12 deletions spec/Resolvable/Resolver/AwsS3PresignedUrlResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,4 @@ function it_is_a_resolver()
{
$this->shouldImplement(ResolverInterface::class);
}

function it_resolves_object_path_into_presigned_url(
$client,
CommandInterface $command,
RequestInterface $presignedRequest
) {
$client->getCommand('GetObject', ['Bucket' => 'bucket', 'Key' => 'base/dir/foo.png'])->willReturn($command);
$client->createPresignedRequest($command, Argument::type(\DateTime::class))->willReturn($presignedRequest);
$presignedRequest->getUri()->willReturn('https://amazon');

$this->resolve('/foo.png')->shouldReturn('https://amazon');
}
}
20 changes: 20 additions & 0 deletions src/Resolvable/ResolvableFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@

use Gaufrette\FilesystemInterface;

/**
* Filesystem decorator providing resolve() method to resolve an object path into URI.
*
* Example:
* $client = // AwsS3 client instantiation
* $decorated = new Filesystem(new AwsS3($client, 'my_bucket', ['directory' => 'root/dir']));
* $filesystem = new ResolvableFilesystem(
* $decorated,
* new AwsS3PresignedUrlResolver($client, 'my_bucket', 'root/dir')
* );
*
* try {
* // should return something like "https://eu-west-1.blabla.aws.com/my_bucket/root/dir/foo/bar.png?token
* var_dump($filesystem->resolve('foo/bar.png'));
* } catch (ResolverException $e) {
*
* }
*
* @author Albin Kerouanton <[email protected]>
*/
class ResolvableFilesystem implements FilesystemInterface
{
/** @var FilesystemInterface */
Expand Down
14 changes: 13 additions & 1 deletion src/Resolvable/Resolver/AwsS3PresignedUrlResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/**
* Resolves object paths into time-limited URLs, namely presigned URLs.
*
* @author Albin Kerouanton <[email protected]>
*
* @see http://docs.aws.amazon.com/AmazonS3/latest/dev/ShareObjectPreSignedURL.html
*/
class AwsS3PresignedUrlResolver implements ResolverInterface
Expand Down Expand Up @@ -43,10 +45,20 @@ public function __construct(S3Client $service, $bucket, $baseDir, \DateTimeInter
*
* @param string $path
*
* @return (string) \Psr\Http\Message\UriInterface
* @return string
*/
public function resolve($path)
{
// For AWS SDK v2
if ($this->service instanceof \Aws\Common\Client\AbstractClient) {
return $this->service->getObjectUrl(
$this->bucket,
$this->computePath($path),
$this->expiresAt
);
}

// For AWS SDK v3
$command = $this->service->getCommand('GetObject', [
'Bucket' => $this->bucket,
'Key' => $this->computePath($path),
Expand Down
4 changes: 3 additions & 1 deletion src/Resolvable/Resolver/AwsS3PublicUrlResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Gaufrette\Extras\Resolvable\ResolverInterface;

/**
* Resolves objects' path into public URLs.
* Resolves path of public objects into URLs.
*
* @author Albin Kerouanton <[email protected]>
*/
class AwsS3PublicUrlResolver implements ResolverInterface
{
Expand Down
2 changes: 2 additions & 0 deletions src/Resolvable/Resolver/StaticUrlResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/**
* Resolves object path by prepending a static prefix.
*
* @author Albin Kerouanton <[email protected]>
*/
class StaticUrlResolver implements ResolverInterface
{
Expand Down
5 changes: 5 additions & 0 deletions src/Resolvable/ResolverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Gaufrette\Extras\Resolvable;

/**
* Base exception thrown by resolvers if they fail to resolve an object path into URI.
*
* @author Albin Kerouanton <[email protected]>
*/
class ResolverException extends \Exception
{
}
5 changes: 5 additions & 0 deletions src/Resolvable/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Gaufrette\Extras\Resolvable;

/**
* Resolves an object path into an URI.
*
* @author Albin Kerouanton <[email protected]>
*/
interface ResolverInterface
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Resolvable/UnresolvableObjectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Gaufrette\Extras\Resolvable;

/**
* Exception thrown by the filesystem when resolution fail.
*
* @author Albin Kerouanton <[email protected]>
*/
class UnresolvableObjectException extends \Exception
{
/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Resolvable/Resolver/AwsS3SetUpTearDownTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ public function setUp()
}

$this->bucket = uniqid(getenv('AWS_BUCKET'));

// For AWS SDK v2
if (class_exists('Aws\Common\Client\AbstractClient')) {
return $this->client = S3Client::factory([
'region' => $region ? $region : 'eu-west-1',
'version' => '2006-03-01',
'key' => $key,
'secret' => $secret,
]);
}

// For AWS SDK v3
$this->client = new S3Client([
'region' => $region ? $region : 'eu-west-1',
'version' => 'latest',
Expand Down

0 comments on commit 6a0c0e2

Please sign in to comment.