rewrote initialisation, added testing workaround

This commit is contained in:
Christian Berendt 2013-07-10 07:47:15 +02:00
parent 020803aa7e
commit 203df66c00
3 changed files with 49 additions and 28 deletions

View File

@ -36,6 +36,8 @@ class AmazonS3 extends \OC\Files\Storage\Common {
private $connection; private $connection;
private $bucket; private $bucket;
private static $tmpFiles = array(); private static $tmpFiles = array();
private $test = false;
private $timeout = 15;
private function normalizePath($path) { private function normalizePath($path) {
$path = trim($path, '/'); $path = trim($path, '/');
@ -47,44 +49,54 @@ class AmazonS3 extends \OC\Files\Storage\Common {
return $path; return $path;
} }
private function testTimeout() {
if ($this->test) {
sleep($this->timeout);
}
}
public function __construct($params) { public function __construct($params) {
if ( ! isset($params['key']) || ! isset($params['secret']) || ! isset($params['bucket'])) { if ( ! isset($params['key']) || ! isset($params['secret']) || ! isset($params['bucket'])) {
throw new \Exception("Access Key, Secret and Bucket have to be configured."); throw new \Exception("Access Key, Secret and Bucket have to be configured.");
} }
$this->id = 'amazon::' . $params['key'] . md5($params['secret']); $this->id = 'amazon::' . $params['key'] . md5($params['secret']);
$this->bucket = $params['bucket']; $this->bucket = $params['bucket'];
$scheme = ($params['use_ssl'] === 'false') ? 'http' : 'https'; $scheme = ($params['use_ssl'] === 'false') ? 'http' : 'https';
$this->test = ( isset($params['test'])) ? true : false;
if (isset($params['hostname']) && isset($params['port'])) { $this->timeout = ( ! isset($params['timeout'])) ? 15 : $params['timeout'];
$base_url = $scheme.'://'.$params['hostname'].':'.$params['port'].'/'; $params['region'] = ( ! isset($params['region'])) ? 'eu-west-1' : $params['region'];
$this->connection = S3Client::factory(array( $params['hostname'] = ( !isset($params['hostname'])) ? 's3.amazonaws.com' : $params['hostname'];
'key' => $params['key'], if ( ! isset($params['port'])) {
'secret' => $params['secret'], $params['port'] = ($params['use_ssl'] === 'false') ? 80 : 443;
'base_url' => $base_url
));
} else {
if ( ! isset($params['region'])) {
$params['region'] = 'us-west-1';
}
$this->connection = S3Client::factory(array(
'key' => $params['key'],
'secret' => $params['secret'],
'scheme' => $scheme,
'region' => $params['region']
));
} }
$base_url = $scheme.'://'.$params['hostname'].':'.$params['port'].'/';
$this->connection = S3Client::factory(array(
'key' => $params['key'],
'secret' => $params['secret'],
'base_url' => $base_url,
'region' => $params['region']
));
if (! $this->connection->isValidBucketName($this->bucket)) { if (! $this->connection->isValidBucketName($this->bucket)) {
throw new \Exception("The configured bucket name is invalid."); throw new \Exception("The configured bucket name is invalid.");
} }
if ( ! $this->connection->doesBucketExist($this->bucket)) { if ( ! $this->connection->doesBucketExist($this->bucket)) {
$result = $this->connection->createBucket(array( try {
'Bucket' => $this->bucket $result = $this->connection->createBucket(array(
)); 'Bucket' => $this->bucket
while ( ! $this->connection->doesBucketExist($this->bucket)) { ));
sleep(1); $this->connection->waitUntilBucketExists(array(
'Bucket' => $this->bucket,
'waiter.interval' => 1,
'waiter.max_attempts' => 15
));
$this->testTimeout();
} catch (S3Exception $e) {
throw new \Exception("Creation of bucket failed.");
} }
} }
@ -96,6 +108,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'ContentType' => 'httpd/unix-directory', 'ContentType' => 'httpd/unix-directory',
'ContentLength' => 0 'ContentLength' => 0
)); ));
$this->testTimeout();
} }
} }
@ -114,6 +127,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'ContentType' => 'httpd/unix-directory', 'ContentType' => 'httpd/unix-directory',
'ContentLength' => 0 'ContentLength' => 0
)); ));
$this->testTimeout();
} catch (S3Exception $e) { } catch (S3Exception $e) {
return false; return false;
} }
@ -168,6 +182,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'Bucket' => $this->bucket, 'Bucket' => $this->bucket,
'Key' => $path . '/' 'Key' => $path . '/'
)); ));
$this->testTimeout();
} catch (S3Exception $e) { } catch (S3Exception $e) {
return false; return false;
} }
@ -276,6 +291,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'Bucket' => $this->bucket, 'Bucket' => $this->bucket,
'Key' => $path 'Key' => $path
)); ));
$this->testTimeout();
} catch (S3Exception $e) { } catch (S3Exception $e) {
return false; return false;
} }
@ -373,12 +389,14 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'Metadata' => $metadata, 'Metadata' => $metadata,
'CopySource' => $this->bucket . '/' . $path 'CopySource' => $this->bucket . '/' . $path
)); ));
$this->testTimeout();
} else { } else {
$result = $this->connection->putObject(array( $result = $this->connection->putObject(array(
'Bucket' => $this->bucket, 'Bucket' => $this->bucket,
'Key' => $path, 'Key' => $path,
'Metadata' => $metadata 'Metadata' => $metadata
)); ));
$this->testTimeout();
} }
} catch (S3Exception $e) { } catch (S3Exception $e) {
return false; return false;
@ -398,6 +416,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'Key' => $path2, 'Key' => $path2,
'CopySource' => $this->bucket . '/' . $path1 'CopySource' => $this->bucket . '/' . $path1
)); ));
$this->testTimeout();
} catch (S3Exception $e) { } catch (S3Exception $e) {
return false; return false;
} }
@ -412,6 +431,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'Key' => $path2 . '/', 'Key' => $path2 . '/',
'CopySource' => $this->bucket . '/' . $path1 . '/' 'CopySource' => $this->bucket . '/' . $path1 . '/'
)); ));
$this->testTimeout();
} catch (S3Exception $e) { } catch (S3Exception $e) {
return false; return false;
} }
@ -491,6 +511,7 @@ class AmazonS3 extends \OC\Files\Storage\Common {
'ContentType' => \OC_Helper::getMimeType($tmpFile), 'ContentType' => \OC_Helper::getMimeType($tmpFile),
'ContentLength' => filesize($tmpFile) 'ContentLength' => filesize($tmpFile)
)); ));
$this->testTimeout();
unlink($tmpFile); unlink($tmpFile);
$this->touch(dirname(self::$tmpFiles[$tmpFile])); $this->touch(dirname(self::$tmpFiles[$tmpFile]));

View File

@ -59,9 +59,8 @@ class AmazonS3 extends Storage {
'Bucket' => $this->config['amazons3']['bucket'] 'Bucket' => $this->config['amazons3']['bucket']
)); ));
while($connection->doesBucketExist($this->config['amazons3']['bucket'])) { //wait some seconds for completing the replication
sleep(1); sleep(30);
}
} }
} }
} }

View File

@ -54,8 +54,9 @@ return array(
//'hostname' => 'your.host.name', //'hostname' => 'your.host.name',
//'port' => '443', //'port' => '443',
//'use_ssl' => 'true', //'use_ssl' => 'true',
//'use_path_style' => 'false', //'region' => 'eu-west-1',
//'region' => 'us-west-1' //'test'=>'true',
//'timeout'=>20
), ),
'dropbox' => array ( 'dropbox' => array (
'run'=>false, 'run'=>false,