Skip to content

Commit

Permalink
Use doesObjectExist again, extracted prefix check logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Apr 19, 2016
1 parent 891dbb8 commit 7384943
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
21 changes: 6 additions & 15 deletions spec/AwsS3AdapterSpec.php
Expand Up @@ -17,6 +17,9 @@

class AwsS3AdapterSpec extends ObjectBehavior
{
/**
* @var \Aws\S3\S3Client
*/
private $client;
private $bucket;
const PATH_PREFIX = 'path-prefix';
Expand Down Expand Up @@ -163,11 +166,7 @@ public function it_should_return_true_when_object_exists($command)
{
$key = 'key.txt';
$result = new Result();
$this->client->getCommand('headObject', [
'Bucket' => $this->bucket,
'Key' => self::PATH_PREFIX.'/'.$key
])->willReturn($command);
$this->client->execute($command)->willReturn($result);
$this->client->doesObjectExist($this->bucket, self::PATH_PREFIX.'/'.$key)->willReturn(true);

$this->has($key)->shouldBe(true);
}
Expand All @@ -183,11 +182,7 @@ public function it_should_return_true_when_prefix_exists($command)
'Key' => 'directory/foo.txt',
],
]);
$this->client->getCommand('headObject', [
'Bucket' => $this->bucket,
'Key' => self::PATH_PREFIX.'/'.$key
])->willReturn($command);
$this->client->execute($command)->willThrow(S3Exception::class);
$this->client->doesObjectExist($this->bucket, self::PATH_PREFIX.'/'.$key)->willReturn(false);

$this->client->getCommand('listObjects', [
'Bucket' => $this->bucket,
Expand Down Expand Up @@ -535,11 +530,7 @@ public function getMatchers()

private function make_it_404_on_has_object($headCommand, $listCommand, $key)
{
$this->client->getCommand('headObject', [
'Bucket' => $this->bucket,
'Key' => self::PATH_PREFIX.'/'.$key
])->willReturn($headCommand);
$this->client->execute($headCommand)->willThrow(S3Exception::class);
$this->client->doesObjectExist($this->bucket, self::PATH_PREFIX.'/'.$key)->willReturn(false);

$result = new Result();
$this->client->getCommand('listObjects', [
Expand Down
52 changes: 26 additions & 26 deletions src/AwsS3Adapter.php
Expand Up @@ -204,34 +204,11 @@ public function has($path)
{
$location = $this->applyPathPrefix($path);

try {
$command = $this->s3Client->getCommand(
'headObject',
[
'Bucket' => $this->bucket,
'Key' => $location,
]
);

$this->s3Client->execute($command);

if ($this->s3Client->doesObjectExist($this->bucket, $location)) {
return true;
} catch (S3Exception $e) {
// Maybe this isn't an actual key, but a prefix.
// Do a prefix listing of objects to determine.
$command = $this->s3Client->getCommand(
'listObjects',
[
'Bucket' => $this->bucket,
'Prefix' => rtrim($location, '/').'/',
'MaxKeys' => 1,
]
);

$result = $this->s3Client->execute($command);

return $result['Contents'] || $result['CommonPrefixes'];
}

return $this->doesDirectoryExist($location);
}

/**
Expand Down Expand Up @@ -653,4 +630,27 @@ protected function normalizeResponse(array $response, $path = null)

return array_merge($result, Util::map($response, static::$resultMap), ['type' => 'file']);
}

/**
* @param $location
*
* @return bool
*/
protected function doesDirectoryExist($location)
{
// Maybe this isn't an actual key, but a prefix.
// Do a prefix listing of objects to determine.
$command = $this->s3Client->getCommand(
'listObjects',
[
'Bucket' => $this->bucket,
'Prefix' => rtrim($location, '/') . '/',
'MaxKeys' => 1,
]
);

$result = $this->s3Client->execute($command);

return $result['Contents'] || $result['CommonPrefixes'];
}
}

0 comments on commit 7384943

Please sign in to comment.