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);
+
+}
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)