$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());
- }
- }
}
/**
}
try {
- $this->connection->putObject(array(
+ $this->getConnection()->putObject(array(
'Bucket' => $this->bucket,
'Key' => $path . '/',
'ContentType' => 'httpd/unix-directory'
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) {
// 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']
));
try {
$files = array();
- $result = $this->connection->getIterator('ListObjects', array(
+ $result = $this->getConnection()->getIterator('ListObjects', array(
'Bucket' => $this->bucket,
'Delimiter' => '/',
'Prefix' => $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
));
}
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) {
}
try {
- $this->connection->deleteObject(array(
+ $this->getConnection()->deleteObject(array(
'Bucket' => $this->bucket,
'Key' => $path
));
self::$tmpFiles[$tmpFile] = $path;
try {
- $this->connection->getObject(array(
+ $this->getConnection()->getObject(array(
'Bucket' => $this->bucket,
'Key' => $path,
'SaveAs' => $tmpFile
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
));
if ($fileType === 'dir' && ! $this->isRoot($path)) {
$path .= '/';
}
- $this->connection->copyObject(array(
+ $this->getConnection()->copyObject(array(
'Bucket' => $this->bucket,
'Key' => $this->cleanKey($path),
'Metadata' => $metadata,
$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,
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)
$this->remove($path2);
try {
- $this->connection->copyObject(array(
+ $this->getConnection()->copyObject(array(
'Bucket' => $this->bucket,
'Key' => $path2 . '/',
'CopySource' => S3Client::encodeKey($this->bucket . '/' . $path1 . '/')
}
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'))) {
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'],
+ '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());
+ }
+ }
+
return $this->connection;
}
}
try {
- $this->connection->putObject(array(
+ $this->getConnection()->putObject(array(
'Bucket' => $this->bucket,
'Key' => $this->cleanKey(self::$tmpFiles[$tmpFile]),
'SourceFile' => $tmpFile,
$this->id = 'dropbox::'.$params['app_key'] . $params['token']. '/' . $this->root;
$oauth = new \Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']);
$oauth->setToken($params['token'], $params['token_secret']);
+ // note: Dropbox_API connection is lazy
$this->dropbox = new \Dropbox_API($oauth, 'auto');
} else {
throw new \Exception('Creating \OC\Files\Storage\Dropbox storage failed');
$this->root .= '/';
}
} else {
- throw new \Exception();
+ throw new \Exception('Creating \OC\Files\Storage\FTP storage failed');
}
}
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setUseObjects(true);
$client->setAccessToken($params['token']);
+ // note: API connection is lazy
$this->service = new \Google_DriveService($client);
$token = json_decode($params['token'], true);
$this->id = 'google::'.substr($params['client_id'], 0, 30).$token['created'];
if (substr($this->root, -1, 1) != '/') {
$this->root .= '/';
}
+ }
+
+ /**
+ * Returns the connection.
+ *
+ * @return \Net_SFTP connected client instance
+ * @throws \Exception when the connection failed
+ */
+ public function getConnection() {
+ if (!is_null($this->client)) {
+ return $this->client;
+ }
$hostKeys = $this->readHostKeys();
$this->client = new \Net_SFTP($this->host);
if (!$this->client->login($this->user, $this->password)) {
throw new \Exception('Login failed');
}
+ return $this->client;
}
public function test() {
) {
return false;
}
- return $this->client->nlist() !== false;
+ return $this->getConnection()->nlist() !== false;
}
public function getId(){
public function mkdir($path) {
try {
- return $this->client->mkdir($this->absPath($path));
+ return $this->getConnection()->mkdir($this->absPath($path));
} catch (\Exception $e) {
return false;
}
public function rmdir($path) {
try {
- return $this->client->delete($this->absPath($path), true);
+ return $this->getConnection()->delete($this->absPath($path), true);
} catch (\Exception $e) {
return false;
}
public function opendir($path) {
try {
- $list = $this->client->nlist($this->absPath($path));
+ $list = $this->getConnection()->nlist($this->absPath($path));
if ($list === false) {
return false;
}
public function filetype($path) {
try {
- $stat = $this->client->stat($this->absPath($path));
+ $stat = $this->getConnection()->stat($this->absPath($path));
if ($stat['type'] == NET_SFTP_TYPE_REGULAR) {
return 'file';
}
public function file_exists($path) {
try {
- return $this->client->stat($this->absPath($path)) !== false;
+ return $this->getConnection()->stat($this->absPath($path)) !== false;
} catch (\Exception $e) {
return false;
}
public function unlink($path) {
try {
- return $this->client->delete($this->absPath($path), true);
+ return $this->getConnection()->delete($this->absPath($path), true);
} catch (\Exception $e) {
return false;
}
case 'x+':
case 'c':
case 'c+':
- $context = stream_context_create(array('sftp' => array('session' => $this->client)));
+ $context = stream_context_create(array('sftp' => array('session' => $this->getConnection())));
return fopen($this->constructUrl($path), $mode, false, $context);
}
} catch (\Exception $e) {
return false;
}
if (!$this->file_exists($path)) {
- $this->client->put($this->absPath($path), '');
+ $this->getConnection()->put($this->absPath($path), '');
} else {
return false;
}
}
public function getFile($path, $target) {
- $this->client->get($path, $target);
+ $this->getConnection()->get($path, $target);
}
public function uploadFile($path, $target) {
- $this->client->put($target, $path, NET_SFTP_LOCAL_FILE);
+ $this->getConnection()->put($target, $path, NET_SFTP_LOCAL_FILE);
}
public function rename($source, $target) {
if (!$this->is_dir($target) && $this->file_exists($target)) {
$this->unlink($target);
}
- return $this->client->rename(
+ return $this->getConnection()->rename(
$this->absPath($source),
$this->absPath($target)
);
public function stat($path) {
try {
- $stat = $this->client->stat($this->absPath($path));
+ $stat = $this->getConnection()->stat($this->absPath($path));
$mtime = $stat ? $stat['mtime'] : -1;
$size = $stat ? $stat['size'] : 0;
* @var string
*/
private $bucket;
+ /**
+ * Connection parameters
+ *
+ * @var array
+ */
+ private $params;
/**
* @var array
*/
*/
private function doesObjectExist($path) {
try {
- $this->container->getPartialObject($path);
+ $this->getContainer()->getPartialObject($path);
return true;
} catch (ClientErrorResponseException $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
$params['service_name'] = 'cloudFiles';
}
- $settings = array(
- 'username' => $params['user'],
- );
-
- if (!empty($params['password'])) {
- $settings['password'] = $params['password'];
- } else if (!empty($params['key'])) {
- $settings['apiKey'] = $params['key'];
- }
-
- if (!empty($params['tenant'])) {
- $settings['tenantName'] = $params['tenant'];
- }
-
- if (!empty($params['timeout'])) {
- $settings['timeout'] = $params['timeout'];
- }
-
- if (isset($settings['apiKey'])) {
- $this->anchor = new Rackspace($params['url'], $settings);
- } else {
- $this->anchor = new OpenStack($params['url'], $settings);
- }
-
- $this->connection = $this->anchor->objectStoreService($params['service_name'], $params['region']);
-
- try {
- $this->container = $this->connection->getContainer($this->bucket);
- } catch (ClientErrorResponseException $e) {
- $this->container = $this->connection->createContainer($this->bucket);
- }
-
- if (!$this->file_exists('.')) {
- $this->mkdir('.');
- }
+ $this->params = $params;
}
public function mkdir($path) {
$customHeaders = array('content-type' => 'httpd/unix-directory');
$metadataHeaders = DataObject::stockHeaders(array());
$allHeaders = $customHeaders + $metadataHeaders;
- $this->container->uploadObject($path, '', $allHeaders);
+ $this->getContainer()->uploadObject($path, '', $allHeaders);
} catch (Exceptions\CreateUpdateError $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
try {
- $this->container->dataObject()->setName($path . '/')->delete();
+ $this->getContainer()->dataObject()->setName($path . '/')->delete();
} catch (Exceptions\DeleteError $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
try {
$files = array();
/** @var OpenCloud\Common\Collection $objects */
- $objects = $this->container->objectList(array(
+ $objects = $this->getContainer()->objectList(array(
'prefix' => $path,
'delimiter' => '/'
));
try {
/** @var DataObject $object */
- $object = $this->container->getPartialObject($path);
+ $object = $this->getContainer()->getPartialObject($path);
} catch (ClientErrorResponseException $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
}
try {
- $this->container->dataObject()->setName($path)->delete();
+ $this->getContainer()->dataObject()->setName($path)->delete();
} catch (ClientErrorResponseException $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
$tmpFile = \OC_Helper::tmpFile();
self::$tmpFiles[$tmpFile] = $path;
try {
- $object = $this->container->getObject($path);
+ $object = $this->getContainer()->getObject($path);
} catch (ClientErrorResponseException $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return false;
if ($this->is_dir($path)) {
return 'httpd/unix-directory';
} else if ($this->file_exists($path)) {
- $object = $this->container->getPartialObject($path);
+ $object = $this->getContainer()->getPartialObject($path);
return $object->getContentType();
}
return false;
$path .= '/';
}
- $object = $this->container->getPartialObject($path);
+ $object = $this->getContainer()->getPartialObject($path);
$object->saveMetadata($metadata);
return true;
} else {
$customHeaders = array('content-type' => $mimeType);
$metadataHeaders = DataObject::stockHeaders($metadata);
$allHeaders = $customHeaders + $metadataHeaders;
- $this->container->uploadObject($path, '', $allHeaders);
+ $this->getContainer()->uploadObject($path, '', $allHeaders);
return true;
}
}
$this->unlink($path2);
try {
- $source = $this->container->getPartialObject($path1);
+ $source = $this->getContainer()->getPartialObject($path1);
$source->copy($this->bucket . '/' . $path2);
} catch (ClientErrorResponseException $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
$this->unlink($path2);
try {
- $source = $this->container->getPartialObject($path1 . '/');
+ $source = $this->getContainer()->getPartialObject($path1 . '/');
$source->copy($this->bucket . '/' . $path2 . '/');
} catch (ClientErrorResponseException $e) {
\OCP\Util::writeLog('files_external', $e->getMessage(), \OCP\Util::ERROR);
return $this->id;
}
+ /**
+ * Returns the connection
+ *
+ * @return OpenCloud\ObjectStore\Service connected client
+ * @throws \Exception if connection could not be made
+ */
public function getConnection() {
+ if (!is_null($this->connection)) {
+ return $this->connection;
+ }
+
+ $settings = array(
+ 'username' => $this->params['user'],
+ );
+
+ if (!empty($this->params['password'])) {
+ $settings['password'] = $this->params['password'];
+ } else if (!empty($this->params['key'])) {
+ $settings['apiKey'] = $this->params['key'];
+ }
+
+ if (!empty($this->params['tenant'])) {
+ $settings['tenantName'] = $this->params['tenant'];
+ }
+
+ if (!empty($this->params['timeout'])) {
+ $settings['timeout'] = $this->params['timeout'];
+ }
+
+ if (isset($settings['apiKey'])) {
+ $this->anchor = new Rackspace($this->params['url'], $settings);
+ } else {
+ $this->anchor = new OpenStack($this->params['url'], $settings);
+ }
+
+ $this->connection = $this->anchor->objectStoreService($this->params['service_name'], $this->params['region']);
+
return $this->connection;
}
+ /**
+ * Returns the initialized object store container.
+ *
+ * @return OpenCloud\ObjectStore\Resource\Container
+ */
+ public function getContainer() {
+ if (!is_null($this->container)) {
+ return $this->container;
+ }
+
+ try {
+ $this->container = $this->getConnection()->getContainer($this->bucket);
+ } catch (ClientErrorResponseException $e) {
+ $this->container = $this->getConnection()->createContainer($this->bucket);
+ }
+
+ if (!$this->file_exists('.')) {
+ $this->mkdir('.');
+ }
+
+ return $this->container;
+ }
+
public function writeBack($tmpFile) {
if (!isset(self::$tmpFiles[$tmpFile])) {
return false;
}
$fileData = fopen($tmpFile, 'r');
- $this->container->uploadObject(self::$tmpFiles[$tmpFile], $fileData);
+ $this->getContainer()->uploadObject(self::$tmpFiles[$tmpFile], $fileData);
unlink($tmpFile);
}