Permalink
Browse files

Update AmazonS3 adapter

  • Loading branch information...
1 parent cad9ed2 commit 8cee18d5285a14d6ec2c9d460a312da266e81816 @Herzult Herzult committed Apr 25, 2012
Showing with 123 additions and 188 deletions.
  1. +112 −118 src/Gaufrette/Adapter/AmazonS3.php
  2. +1 −70 tests/Gaufrette/Adapter/AmazonS3Test.php
  3. +10 −0 tests/adapters/AmazonS3.php.dist
@@ -2,6 +2,8 @@
namespace Gaufrette\Adapter;
+use Gaufrette\Exception;
+
/**
* Amazon S3 adapter
*
@@ -26,36 +28,42 @@ public function __construct(\AmazonS3 $service, $bucket, $create = false)
/**
* Set the base directory the user will have access to
*
- * @param String $directory
+ * @param string $directory
*/
public function setDirectory($directory)
{
- $this->directory = $directory;
+ $this->directory = $directory;
}
/**
* Get the directory the user has access to
*
- * @return String
+ * @return string
*/
public function getDirectory()
{
return $this->directory;
}
-
/**
* {@inheritDoc}
*/
public function read($key)
{
$this->ensureBucketExists();
- $key = $this->prependBaseDirectory($key);
+ $response = $this->service->get_object(
+ $this->bucket,
+ $this->computePath($key)
+ );
- $response = $this->service->get_object($this->bucket, $key);
- if (!$response->isOK()) {
- throw new \RuntimeException(sprintf('Could not read the \'%s\' file.', $key));
+ if (404 === $response->status) {
+ throw new Exception\FileNotFound($key);
+ } elseif (!$response->isOK()) {
+ throw new \RuntimeException(sprintf(
+ 'Could not read the "%s" file.',
+ $key
+ ));
}
return $response->body;
@@ -66,22 +74,27 @@ public function read($key)
*/
public function rename($key, $new)
{
- $key = $this->prependBaseDirectory($key);
- $new = $this->prependBaseDirectory($new);
-
- $source = array(
- "bucket" => $this->bucket,
- "filename" => $key,
- );
+ $this->ensureBucketExists();
- $destination = array(
- "bucket" => $this->bucket,
- "filename" => $new,
+ $response = $this->service->copy_object(
+ array( // source
+ 'bucket' => $this->bucket,
+ 'filename' => $this->computePath($key)
+ ),
+ array( // target
+ 'bucket' => $this->bucket,
+ 'filename' => $this->computePath($key)
+ )
);
- $response = $this->service->copy_object($source, $destination);
- if (!$response->isOK()) {
- throw new \RuntimeException(sprintf('Could not rename the \'%s\' file.', $key));
+ if (404 === $response->status) {
+ throw new Exception\FileNotFound($key);
+ } elseif (!$response->isOK()) {
+ throw new \RuntimeException(sprintf(
+ 'Could not rename the "%s" file into "%s".',
+ $key,
+ $new
+ ));
}
$this->delete($key);
@@ -92,42 +105,16 @@ public function rename($key, $new)
*/
public function write($key, $content, array $metadata = null)
{
- $key = $this->prependBaseDirectory($key);
-
$this->ensureBucketExists();
- $opt = array("body" => $content);
-
- if (null !== $metadata) {
@schmittjoh

schmittjoh Sep 21, 2012

Contributor

Why was all the header handling removed?

- $opt['meta'] = array();
- foreach ($metadata as $k => $v) {
- $lk = strtolower($k);
-
- if ('content-type' === $lk) {
- $opt['contentType'] = $v;
- continue;
- }
-
- if ('expires' === $lk) {
- $opt['headers']['Expires'] = $v;
- continue;
- }
+ $opt = array('body' => $content);
- if ('cache-control' === $lk) {
- $opt['headers']['Cache-Control'] = $v;
- continue;
- }
-
- if ('content-encoding' === $lk) {
- $opt['headers']['Content-Encoding'] = $v;
- continue;
- }
-
- $opt['meta'][$k] = $v;
- }
- }
+ $response = $this->service->create_object(
+ $this->bucket,
+ $this->computePath($key),
+ array('body' => $content)
@neubi4

neubi4 Jun 13, 2012

Should it not be $opt?

+ );
- $response = $this->service->create_object($this->bucket, $key, $opt);
if (!$response->isOK()) {
throw new \RuntimeException(sprintf('Could not write the \'%s\' file.', $key));
}
@@ -140,55 +127,32 @@ public function write($key, $content, array $metadata = null)
*/
public function exists($key)
{
- $key = $this->prependBaseDirectory($key);
-
$this->ensureBucketExists();
- return $this->service->if_object_exists($this->bucket, $key);
+ return $this->service->if_object_exists(
+ $this->bucket,
+ $this->computePath($key)
+ );
}
/**
* {@inheritDoc}
*/
public function mtime($key)
{
- $key = $this->prependBaseDirectory($key);
+ $response = $this->getObjectMetadata($key);
- $headers = $this->getHeaders($key);
-
- return strtotime($headers['last-modified']);
+ return strtotime($response['Headers']['last-modified']);
}
/**
* {@inheritDoc}
*/
public function checksum($key)
{
- $key = $this->prependBaseDirectory($key);
-
- $headers = $this->getHeaders($key);
-
- return strtotime($headers['etag']);
- }
-
- /**
- * Fetch the headers of an object
- *
- * @param type $key Object of which to get the headers
- * @return type array Object headers
- */
- protected function getHeaders($key)
- {
- $key = $this->prependBaseDirectory($key);
-
- $this->ensureBucketExists();
- $response = $this->service->get_object_metadata($this->bucket, $key);
-
- if ($response === false) {
- throw new \RuntimeException(sprintf('The \'%s\' file does not exist.', $key));
- }
+ $response = $this->getObjectMetadata($key);
- return $response["Headers"];
+ return trim($response['ETag'], '"');
}
/**
@@ -218,20 +182,33 @@ public function delete($key)
{
$this->ensureBucketExists();
- $response = $this->service->delete_object($this->bucket, $this->prependBaseDirectory($key));
+ $response = $this->service->delete_object(
+ $this->bucket,
+ $this->computePath($key)
+ );
+
if (!$response->isOK()) {
- throw new \RuntimeException(sprintf('Could not delete the \'%s\' file.', $key));
+ throw new \RuntimeException(sprintf(
+ 'Could not delete the "%s" file.',
+ $key
+ ));
}
}
- public function prependBaseDirectory($key)
+ private function getObjectMetadata($key)
{
- if (!$directory = $this->getDirectory()) {
+ $this->ensureBucketExists();
- return $key;
+ $response = $this->service->get_object_metadata(
+ $this->bucket,
+ $this->computePath($key)
+ );
+
+ if (false === $response) {
+ throw new Exception\FileNotFound($key);
}
- return $directory . '/' . $key;
+ return $response;
}
/**
@@ -242,23 +219,37 @@ public function prependBaseDirectory($key)
* @throws RuntimeException if the bucket does not exists or could not be
* created
*/
- protected function ensureBucketExists()
+ private function ensureBucketExists()
{
- if (!$this->ensureBucket) {
- $available = $this->service->if_bucket_exists($this->bucket);
-
- if (!$available && $this->create) {
- $response = $this->service->createBucket($this->bucket, \AmazonS3::REGION_US_E1);
- $created = $response->isOK();
- if (!$created) {
- throw new \RuntimeException(sprintf('Could not create the \'%s\' bucket.', $this->bucket));
- }
- } else if (!$available) {
- throw new \RuntimeException(sprintf('The bucket \'%s\' was not found. Please create it on Amazon AWS.', $this->bucket));
- }
-
- $this->ensureBucket = true;
+ if ($this->ensureBucket) {
+ return;
}
+
+ if ($this->service->if_bucket_exists($this->bucket)) {
+ return;
+ }
+
+ if (!$this->create) {
+ throw new \RuntimeException(sprintf(
+ 'The configured bucket "%s" does not exist.',
+ $this->bucket
+ ));
+ }
+
+ // @todo make this region configurable
+ $response = $this->service->create_bucket(
+ $this->bucket,
+ \AmazonS3::REGION_US_E1
+ );
+
+ if (!$response->isOK()) {
+ throw new \RuntimeException(sprintf(
+ 'Failed to create the configured bucket "%s".',
+ $this->bucket
+ ));
+ }
+
+ $this->ensureBucket = true;
}
/**
@@ -268,33 +259,36 @@ protected function ensureBucketExists()
*
* @return string
*/
- public function computePath($key)
+ private function computePath($key)
{
- $path = $this->bucket . '/';
- if ($directory = $this->getDirectory()) {
- $path .= $directory . '/';
+ if (null === $this->directory || '' === $this->directory) {
+ return $key;
}
- $path .= $key;
- return $path;
+ return sprintf('%s/%s', $this->directory, $key);
}
/**
* Computes the key for the specified path
*
* @param string $path for which to compute the key
*/
- public function computeKey($path)
+ private function computeKey($path)
{
- if (0 !== strpos($path, $this->bucket . '/')) {
- throw new \InvalidArgumentException(sprintf('The specified path \'%s\' is out of the bucket \'%s\'.', $path, $this->bucket));
+ if (null === $this->directory || '' === $this->directory) {
+ return $path;
}
- $basePath = $this->bucket;
- if ($directory = $this->getDirectory()) {
- $basePath .= '/' . $directory;
+ $prefix = sprintf('%s/', $this->directory);
+
+ if (0 !== strpos($path, $prefix)) {
+ throw new \InvalidArgumentException(sprintf(
+ 'The specified path "%s" is out of the directory "%s".',
+ $path,
+ $this->directory
+ ));
}
- return ltrim(substr($path, strlen($basePath)), '/');
+ return substr($path, strlen($prefix));
}
}
Oops, something went wrong.

0 comments on commit 8cee18d

Please sign in to comment.