support S3 compatible storage providers (#401)

This commit is contained in:
Saeed Vaziry
2024-12-22 23:06:36 +01:00
committed by GitHub
parent db81583884
commit ea3d64607a
20 changed files with 127 additions and 576 deletions

View File

@ -3,36 +3,31 @@
namespace App\SSH\Storage;
use App\Exceptions\SSHCommandError;
use App\Models\Server;
use App\Models\StorageProvider;
use App\Exceptions\SSHError;
use App\SSH\HasS3Storage;
use App\SSH\HasScripts;
use Illuminate\Support\Facades\Log;
class S3 extends S3AbstractStorage
class S3 extends AbstractStorage
{
use HasS3Storage, HasScripts;
public function __construct(Server $server, StorageProvider $storageProvider)
{
parent::__construct($server, $storageProvider);
$this->setBucketRegion($this->storageProvider->credentials['region']);
$this->setApiUrl();
}
/**
* @throws SSHCommandError
* @throws SSHError
*/
public function upload(string $src, string $dest): array
{
/** @var \App\StorageProviders\S3 $provider */
$provider = $this->storageProvider->provider();
$uploadCommand = $this->getScript('s3/upload.sh', [
'src' => $src,
'bucket' => $this->storageProvider->credentials['bucket'],
'dest' => $this->prepareS3Path($this->storageProvider->credentials['path'].'/'.$dest),
'key' => $this->storageProvider->credentials['key'],
'secret' => $this->storageProvider->credentials['secret'],
'region' => $this->getBucketRegion(),
'endpoint' => $this->getApiUrl(),
'region' => $this->storageProvider->credentials['region'],
'endpoint' => $provider->getApiUrl(),
]);
$upload = $this->server->ssh()->exec($uploadCommand, 'upload-to-s3');
@ -49,20 +44,25 @@ public function upload(string $src, string $dest): array
}
/**
* @throws SSHCommandError
* @throws SSHError
*/
public function download(string $src, string $dest): void
{
/** @var \App\StorageProviders\S3 $provider */
$provider = $this->storageProvider->provider();
$downloadCommand = $this->getScript('s3/download.sh', [
'src' => $this->prepareS3Path($this->storageProvider->credentials['path'].'/'.$src),
'dest' => $dest,
'bucket' => $this->storageProvider->credentials['bucket'],
'key' => $this->storageProvider->credentials['key'],
'secret' => $this->storageProvider->credentials['secret'],
'region' => $this->getBucketRegion(),
'endpoint' => $this->getApiUrl(),
'region' => $this->storageProvider->credentials['region'],
'endpoint' => $provider->getApiUrl(),
]);
Log::info('Downloading from S3', ['command' => $downloadCommand]);
$download = $this->server->ssh()->exec($downloadCommand, 'download-from-s3');
if (! str_contains($download, 'Download successful')) {