From b26bf4aec66005fb2757fc580b5bd4d6e15905a3 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 26 May 2025 20:14:34 +0200 Subject: [PATCH] #7567 --- src/Transfer/GCS.php | 59 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/src/Transfer/GCS.php b/src/Transfer/GCS.php index 14e390a..ecb3f97 100644 --- a/src/Transfer/GCS.php +++ b/src/Transfer/GCS.php @@ -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(); -- 2.39.5