]> _ Git - cubist_net.git/commitdiff
#7567
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 26 May 2025 18:14:34 +0000 (20:14 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 26 May 2025 18:14:34 +0000 (20:14 +0200)
src/Transfer/GCS.php

index 14e390a41a2c9e6c8ae078e70e0da62325f9391b..ecb3f979144d21de9711910dd52183e706453ac8 100644 (file)
@@ -3,16 +3,71 @@
 namespace Cubist\Net;
 
 use Cubist\Net\Transfer\Driver;
-use Cubist\Util\CommandLine\LocalToS3Rclone;
+use Cubist\Util\CommandLine\LocalToGCSRclone;
+use Google\Cloud\Storage\StorageClient;
 
 class GCS extends Driver
 {
+    const ERROR_SERVER_CONNECTION = 6;
+    const ERROR_SEND_FILE = 5;
+
+    /**
+     * @return StorageClient
+     */
+    protected function createClient(&$data)
+    {
+        return new StorageClient();
+    }
+
 
     public function checkConnexion($data = [])
     {
-        // TODO: Implement checkConnexion() method.
+        if(!$this->checkConnection){
+            return true;
+        }
+        $client = $this->createClient($data);
+
+        $test = 'test' . time() . '-' . rand(1, 1000000);
+        $testFile = trim($data['base_path'], '/') . '/' . $test . '.txt';
+
+        $oc = ['Bucket' => $data['bucket'], 'Key' => $testFile];
+
+        try {
+            $client->putObject(array_merge($oc, ['Body' => $test]));
+        } catch (\Exception $e) {
+            return ['error' => true, 'message' => $e->getMessage()];
+        }
+
+        $o = $client->getObject($oc);
+        $ocontent = $o->get('Body');
+
+        $client->deleteObject($oc);
+
+        if ($ocontent == $test) {
+            return true;
+        }
+        return self::ERROR_SEND_FILE;
     }
 
+    protected function _getClientConfig(&$data)
+    {
+        $data = [
+            'access_key' => $data['access_key'] ?? $this->getServer()->getAccessKey(),
+            'secret' => $data['secret'] ?? $this->getServer()->getSecret(),
+            'region' => ($data['region'] ?? $this->getServer()->getRegion()) ?: 'eu-west-3',
+            'bucket' => $data['bucket'] ?? $this->getServer()->getBucket(),
+            'endpoint' => $data['endpoint'] ?? $this->getServer()->getEndpoint(),
+            'base_path' => trim($data['base_path'] ?? $this->getServer()->getBasePath(), "/"),
+            'provider' => $data['s3_provider'] ?? $this->getServer()->getS3Provider(),
+        ];
+
+        return ['region' => $data['region'],
+            'version' => '2006-03-01',
+            'endpoint' => $data['endpoint'],
+            'use_path_style_endpoint' => static::usePathStyleEndpoint($data['provider']),
+            'credentials' => ['key' => $data['access_key'], 'secret' => $data['secret']]
+        ];
+    }
     protected function synchronizeFiles($source, $dest, $mirror = false, $dryrun = false)
     {
         $rclone = new LocalToGCSRclone();