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();