]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix Signiture Does Not Match when mounting Amazon S3 external storage
authorChristopher T. Johnson <ctjctj@gmail.com>
Mon, 14 Apr 2014 13:49:43 +0000 (09:49 -0400)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 8 Jul 2014 12:37:55 +0000 (14:37 +0200)
For some reason the aws-sdk-php package does not caclulate the
signiture correctly when accessing an object in a bucket with a name of
'.'.

When we are at the top of a S3 bucket there is a need(?) to have a directory
name.  Per standard Unix the name picked was '.' (dot or period).  This
choice exercises the aws-sdk bug.

This fix is to add a field to the method to store the name to use instead of
'.' which at this point is hard coded to '<root>'.  We also add a private
function 'cleanKey()' which will test for the '.' name and replace it with
the variable.  Finally all calls to manipulate objects where the path is
not obviously not '.' are processed through cleanKey().

An example where we don't process through clean key would be
'Key' => $path.'/',

Use correct relationship operator

Per feed back use === instead of ==

use '/' instead of '<root>'

apps/files_external/lib/amazons3.php

index 00baacd488cc66b329967ad9caf8c1fd93ce36dd..847b42c27e20002db96f0a6f7e979502d03ae5ca 100644 (file)
@@ -69,6 +69,12 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                        sleep($this->timeout);
                }
        }
+       private function cleanKey($path) {
+               if ($path === '.') {
+                       return '/';
+               }
+               return $path;
+       }
 
        public function __construct($params) {
                if (!isset($params['key']) || !isset($params['secret']) || !isset($params['bucket'])) {
@@ -115,11 +121,10 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                                throw new \Exception("Creation of bucket failed.");
                        }
                }
-
                if (!$this->file_exists('.')) {
                        $result = $this->connection->putObject(array(
                                'Bucket' => $this->bucket,
-                               'Key'    => '.',
+                               'Key'    => $this->cleanKey('.'),
                                'Body'   => '',
                                'ContentType' => 'httpd/unix-directory',
                                'ContentLength' => 0
@@ -164,7 +169,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                try {
                        $result = $this->connection->doesObjectExist(
                                $this->bucket,
-                               $path
+                               $this->cleanKey($path)
                        );
                } catch (S3Exception $e) {
                        \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
@@ -258,7 +263,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
 
                        $result = $this->connection->headObject(array(
                                'Bucket' => $this->bucket,
-                               'Key' => $path
+                               'Key' => $this->cleanKey($path)
                        ));
 
                        $stat = array();
@@ -288,8 +293,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                        if ($path != '.') {
                                $path .= '/';
                        }
-
-                       if ($this->connection->doesObjectExist($this->bucket, $path)) {
+                       if ($this->connection->doesObjectExist($this->bucket, $this->cleanKey($path))) {
                                return 'dir';
                        }
                } catch (S3Exception $e) {
@@ -306,7 +310,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                try {
                        $result = $this->connection->deleteObject(array(
                                'Bucket' => $this->bucket,
-                               'Key' => $path
+                               'Key' => $this->cleanKey($path)
                        ));
                        $this->testTimeout();
                } catch (S3Exception $e) {
@@ -329,7 +333,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                                try {
                                        $result = $this->connection->getObject(array(
                                                'Bucket' => $this->bucket,
-                                               'Key' => $path,
+                                               'Key' => $this->cleanKey($path),
                                                'SaveAs' => $tmpFile
                                        ));
                                } catch (S3Exception $e) {
@@ -377,7 +381,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                        try {
                                $result = $this->connection->headObject(array(
                                        'Bucket' => $this->bucket,
-                                       'Key' => $path
+                                       'Key' => $this->cleanKey($path)
                                ));
                        } catch (S3Exception $e) {
                                \OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
@@ -404,7 +408,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                                }
                                $result = $this->connection->copyObject(array(
                                        'Bucket' => $this->bucket,
-                                       'Key' => $path,
+                                       'Key' => $this->cleanKey($path),
                                        'Metadata' => $metadata,
                                        'CopySource' => $this->bucket . '/' . $path
                                ));
@@ -412,7 +416,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                        } else {
                                $result = $this->connection->putObject(array(
                                        'Bucket' => $this->bucket,
-                                       'Key' => $path,
+                                       'Key' => $this->cleanKey($path),
                                        'Metadata' => $metadata
                                ));
                                $this->testTimeout();
@@ -433,7 +437,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                        try {
                                $result = $this->connection->copyObject(array(
                                        'Bucket' => $this->bucket,
-                                       'Key' => $path2,
+                                       'Key' => $this->cleanKey($path2),
                                        'CopySource' => $this->bucket . '/' . $path1
                                ));
                                $this->testTimeout();
@@ -532,7 +536,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
                try {
                        $result= $this->connection->putObject(array(
                                'Bucket' => $this->bucket,
-                               'Key' => self::$tmpFiles[$tmpFile],
+                               'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]),
                                'SourceFile' => $tmpFile,
                                'ContentType' => \OC_Helper::getMimeType($tmpFile),
                                'ContentLength' => filesize($tmpFile)