-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11694 from owncloud/extstorage-lazyinit
Lazy initialize external storages
- Loading branch information
Showing
6 changed files
with
163 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,34 +111,6 @@ public function __construct($params) { | |
$params['port'] = ($params['use_ssl'] === 'false') ? 80 : 443; | ||
} | ||
$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)) { | ||
throw new \Exception("The configured bucket name is invalid."); | ||
} | ||
|
||
if (!$this->connection->doesBucketExist($this->bucket)) { | ||
try { | ||
$this->connection->createBucket(array( | ||
'Bucket' => $this->bucket | ||
)); | ||
$this->connection->waitUntilBucketExists(array( | ||
'Bucket' => $this->bucket, | ||
'waiter.interval' => 1, | ||
'waiter.max_attempts' => 15 | ||
)); | ||
$this->testTimeout(); | ||
} catch (S3Exception $e) { | ||
\OCP\Util::logException('files_external', $e); | ||
throw new \Exception('Creation of bucket failed. '.$e->getMessage()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -181,7 +153,7 @@ public function mkdir($path) { | |
} | ||
|
||
try { | ||
$this->connection->putObject(array( | ||
$this->getConnection()->putObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $path . '/', | ||
'ContentType' => 'httpd/unix-directory' | ||
|
@@ -216,7 +188,7 @@ public function rmdir($path) { | |
|
||
protected function clearBucket() { | ||
try { | ||
$this->connection->clearBucket($this->bucket); | ||
$this->getConnection()->clearBucket($this->bucket); | ||
return true; | ||
// clearBucket() is not working with Ceph, so if it fails we try the slower approach | ||
} catch (\Exception $e) { | ||
|
@@ -237,9 +209,9 @@ private function batchDelete ($path = null) { | |
// to delete all objects prefixed with the path. | ||
do { | ||
// instead of the iterator, manually loop over the list ... | ||
$objects = $this->connection->listObjects($params); | ||
$objects = $this->getConnection()->listObjects($params); | ||
// ... so we can delete the files in batches | ||
$this->connection->deleteObjects(array( | ||
$this->getConnection()->deleteObjects(array( | ||
'Bucket' => $this->bucket, | ||
'Objects' => $objects['Contents'] | ||
)); | ||
|
@@ -264,7 +236,7 @@ public function opendir($path) { | |
|
||
try { | ||
$files = array(); | ||
$result = $this->connection->getIterator('ListObjects', array( | ||
$result = $this->getConnection()->getIterator('ListObjects', array( | ||
'Bucket' => $this->bucket, | ||
'Delimiter' => '/', | ||
'Prefix' => $path | ||
|
@@ -299,7 +271,7 @@ public function stat($path) { | |
$stat['size'] = -1; //unknown | ||
$stat['mtime'] = time() - $this->rescanDelay * 1000; | ||
} else { | ||
$result = $this->connection->headObject(array( | ||
$result = $this->getConnection()->headObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $path | ||
)); | ||
|
@@ -328,10 +300,10 @@ public function filetype($path) { | |
} | ||
|
||
try { | ||
if ($this->connection->doesObjectExist($this->bucket, $path)) { | ||
if ($this->getConnection()->doesObjectExist($this->bucket, $path)) { | ||
return 'file'; | ||
} | ||
if ($this->connection->doesObjectExist($this->bucket, $path.'/')) { | ||
if ($this->getConnection()->doesObjectExist($this->bucket, $path.'/')) { | ||
return 'dir'; | ||
} | ||
} catch (S3Exception $e) { | ||
|
@@ -350,7 +322,7 @@ public function unlink($path) { | |
} | ||
|
||
try { | ||
$this->connection->deleteObject(array( | ||
$this->getConnection()->deleteObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $path | ||
)); | ||
|
@@ -373,7 +345,7 @@ public function fopen($path, $mode) { | |
self::$tmpFiles[$tmpFile] = $path; | ||
|
||
try { | ||
$this->connection->getObject(array( | ||
$this->getConnection()->getObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $path, | ||
'SaveAs' => $tmpFile | ||
|
@@ -421,7 +393,7 @@ public function getMimeType($path) { | |
return 'httpd/unix-directory'; | ||
} else if ($this->file_exists($path)) { | ||
try { | ||
$result = $this->connection->headObject(array( | ||
$result = $this->getConnection()->headObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $path | ||
)); | ||
|
@@ -449,7 +421,7 @@ public function touch($path, $mtime = null) { | |
if ($fileType === 'dir' && ! $this->isRoot($path)) { | ||
$path .= '/'; | ||
} | ||
$this->connection->copyObject(array( | ||
$this->getConnection()->copyObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $this->cleanKey($path), | ||
'Metadata' => $metadata, | ||
|
@@ -458,7 +430,7 @@ public function touch($path, $mtime = null) { | |
$this->testTimeout(); | ||
} else { | ||
$mimeType = \OC_Helper::getMimetypeDetector()->detectPath($path); | ||
$this->connection->putObject(array( | ||
$this->getConnection()->putObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $this->cleanKey($path), | ||
'Metadata' => $metadata, | ||
|
@@ -481,7 +453,7 @@ public function copy($path1, $path2) { | |
|
||
if ($this->is_file($path1)) { | ||
try { | ||
$this->connection->copyObject(array( | ||
$this->getConnection()->copyObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $this->cleanKey($path2), | ||
'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1) | ||
|
@@ -495,7 +467,7 @@ public function copy($path1, $path2) { | |
$this->remove($path2); | ||
|
||
try { | ||
$this->connection->copyObject(array( | ||
$this->getConnection()->copyObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $path2 . '/', | ||
'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/') | ||
|
@@ -553,7 +525,7 @@ public function rename($path1, $path2) { | |
} | ||
|
||
public function test() { | ||
$test = $this->connection->getBucketAcl(array( | ||
$test = $this->getConnection()->getBucketAcl(array( | ||
'Bucket' => $this->bucket, | ||
)); | ||
if (isset($test) && !is_null($test->getPath('Owner/ID'))) { | ||
|
@@ -566,7 +538,45 @@ public function getId() { | |
return $this->id; | ||
} | ||
|
||
/** | ||
* Returns the connection | ||
* | ||
* @return S3Client connected client | ||
* @throws \Exception if connection could not be made | ||
*/ | ||
public function getConnection() { | ||
if (!is_null($this->connection)) { | ||
return $this->connection; | ||
} | ||
|
||
$this->connection = S3Client::factory(array( | ||
'key' => $params['key'], | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
PVince81
Contributor
|
||
'secret' => $params['secret'], | ||
'base_url' => $base_url, | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
'region' => $params['region'] | ||
)); | ||
|
||
if (!$this->connection->isValidBucketName($this->bucket)) { | ||
throw new \Exception("The configured bucket name is invalid."); | ||
} | ||
|
||
if (!$this->connection->doesBucketExist($this->bucket)) { | ||
try { | ||
$this->connection->createBucket(array( | ||
'Bucket' => $this->bucket | ||
)); | ||
$this->connection->waitUntilBucketExists(array( | ||
'Bucket' => $this->bucket, | ||
'waiter.interval' => 1, | ||
'waiter.max_attempts' => 15 | ||
)); | ||
$this->testTimeout(); | ||
} catch (S3Exception $e) { | ||
\OCP\Util::logException('files_external', $e); | ||
throw new \Exception('Creation of bucket failed. '.$e->getMessage()); | ||
} | ||
} | ||
|
||
return $this->connection; | ||
} | ||
|
||
|
@@ -576,7 +586,7 @@ public function writeBack($tmpFile) { | |
} | ||
|
||
try { | ||
$this->connection->putObject(array( | ||
$this->getConnection()->putObject(array( | ||
'Bucket' => $this->bucket, | ||
'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]), | ||
'SourceFile' => $tmpFile, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
$params['key'] undefined variable