]> _ Git - cubist_net.git/commitdiff
wip #7175 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 5 Nov 2024 14:45:06 +0000 (15:45 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 5 Nov 2024 14:45:06 +0000 (15:45 +0100)
src/Transfer/IServer.php
src/Transfer/S3.php
src/Transfer/S3Compatible.php [new file with mode: 0644]

index 4738209ba5a7ddfe9d144c9d45a52a50221abd82..721ca4c433b0a35ba16fbfd99ef0f9081ab13fc4 100644 (file)
@@ -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);
+
+}
index 9e51851759ad2942ce54c3a80a6d25f02feb9d7a..431efddc9b0df2afc200c4d82d72495a68136a95 100644 (file)
@@ -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 (file)
index 0000000..f41e585
--- /dev/null
@@ -0,0 +1,17 @@
+<?php
+
+namespace Cubist\Net\Transfer;
+
+class S3Compatible extends S3
+{
+
+    protected function _getClientConfig(&$data)
+    {
+        $res = parent::_getClientConfig($data);
+        $res['use_path_style_endpoint'] = true;
+        $res['endpoint'] = $data['endpoint'];
+
+        return $res;
+
+    }
+}