From: Vincent Vanwaelscappel Date: Tue, 5 Nov 2024 14:45:06 +0000 (+0100) Subject: wip #7175 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=cca5e6ce06ab2bdf6443e86a7469d6236d35cf67;p=cubist_net.git wip #7175 @0.5 --- diff --git a/src/Transfer/IServer.php b/src/Transfer/IServer.php index 4738209..721ca4c 100644 --- a/src/Transfer/IServer.php +++ b/src/Transfer/IServer.php @@ -2,47 +2,73 @@ namespace Cubist\Net\Transfer; -interface IServer { - /** - * @return string - */ - public function getProtocol(); - - /** - * @return string - */ - public function getHost(); - - /** - * @return int - */ - public function getPort(); - - /** - * @return string - */ - public function getUsername(); - - /** - * @return string - */ - public function getPassword(); - - /** - * @return string - */ - public function getBasePath(); - - /** - * @return string - */ - public function getBaseURL(); - - /** - * @return array - */ - public function getSettings(); - - public function makeURL($path); - -} \ No newline at end of file +interface IServer +{ + /** + * @return string + */ + public function getProtocol(); + + /** + * @return string + */ + public function getHost(); + + /** + * @return int + */ + public function getPort(); + + /** + * @return string + */ + public function getUsername(); + + /** + * @return string + */ + public function getPassword(); + + /** + * @return string + */ + public function getBasePath(); + + /** + * @return string + */ + public function getBaseURL(); + + /** + * @return array + */ + public function getSettings(); + + /** + * @return string + */ + public function getRegion(); + + /** + * @return string + */ + public function getKey(); + + /** + * @return string + */ + public function getSecret(); + + /** + * @return string + */ + public function getEndpoint(); + + /** + * @return string + */ + public function getBucket(); + + public function makeURL($path); + +} diff --git a/src/Transfer/S3.php b/src/Transfer/S3.php index 9e51851..431efdd 100644 --- a/src/Transfer/S3.php +++ b/src/Transfer/S3.php @@ -2,12 +2,62 @@ namespace Cubist\Net\Transfer; +use Aws\S3\S3Client; + class S3 extends Driver { + const ERROR_SERVER_CONNECTION = 6; + const ERROR_SEND_FILE = 5; + + /** + * @return S3Client + */ + protected function createClient($data) + { + return new S3Client($this->_getClientConfig($data)); + } + public function checkConnexion($data = []) { + $test = 'test' . time(); + $testFile = $data['base_path'] . '/' . $test . '.txt'; + + try { + $client = $this->createClient($data); + } catch (\Exception $e) { + return self::ERROR_SERVER_CONNECTION; + } + + try { + $client->putObject(['Bucket' => $data['bucket'], 'Key' => $testFile, 'Body' => $test]); + } catch (\Exception $e) { + return self::ERROR_SEND_FILE; + } + + $o = $client->getObject(['Bucket' => $data['bucket'], 'Key' => $testFile]); + if ($o->get('Body') == $test) { + return true; + } + return self::ERROR_SEND_FILE; + } + + protected function _getClientConfig(&$data) + { + $data = [ + 'key' => $data['key'] ?? $this->getServer()->getKey(), + 'secret' => $data['secret'] ?? $this->getServer()->getSecret(), + 'region' => $data['region'] ?? $this->getServer()->getRegion(), + 'bucket' => $data['bucket'] ?? $this->getServer()->getBucket(), + 'endpoint' => $data['endpoint'] ?? $this->getServer()->getEndpoint(), + 'base_path' => trim($data['base_path'] ?? $this->getServer()->getBasePath(), "/"), + ]; + return ['region' => $data['region'], + 'version' => '2006-03-01', + 'use_path_style_endpoint' => false, + 'credentials' => ['key' => $data['key'], 'secret' => $data['secret']] + ]; } protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false) diff --git a/src/Transfer/S3Compatible.php b/src/Transfer/S3Compatible.php new file mode 100644 index 0000000..f41e585 --- /dev/null +++ b/src/Transfer/S3Compatible.php @@ -0,0 +1,17 @@ +