mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-17 17:01:37 +00:00
Add S3 and Wasabi as storage providers (#281)
This commit is contained in:
parent
1391eb32d8
commit
e39e8c17a2
@ -9,4 +9,8 @@ final class StorageProvider
|
||||
const FTP = 'ftp';
|
||||
|
||||
const LOCAL = 'local';
|
||||
|
||||
const S3 = 's3';
|
||||
|
||||
const WASABI = 'wasabi';
|
||||
}
|
||||
|
20
app/SSH/HasS3Storage.php
Normal file
20
app/SSH/HasS3Storage.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH;
|
||||
|
||||
trait HasS3Storage
|
||||
{
|
||||
private function prepareS3Path(string $path, string $prefix = ''): string
|
||||
{
|
||||
$path = trim($path);
|
||||
$path = ltrim($path, '/');
|
||||
$path = preg_replace('/[^a-zA-Z0-9\-_\.\/]/', '_', $path);
|
||||
$path = preg_replace('/\/+/', '/', $path);
|
||||
|
||||
if ($prefix) {
|
||||
$path = trim($prefix, '/').'/'.$path;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
78
app/SSH/Storage/S3.php
Normal file
78
app/SSH/Storage/S3.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Storage;
|
||||
|
||||
use App\Exceptions\SSHCommandError;
|
||||
use App\Models\Server;
|
||||
use App\Models\StorageProvider;
|
||||
use App\SSH\HasS3Storage;
|
||||
use App\SSH\HasScripts;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class S3 extends S3AbstractStorage
|
||||
{
|
||||
use HasS3Storage, HasScripts;
|
||||
|
||||
public function __construct(Server $server, StorageProvider $storageProvider)
|
||||
{
|
||||
parent::__construct($server, $storageProvider);
|
||||
$this->setBucketRegion($this->storageProvider->credentials['region']);
|
||||
$this->setApiUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHCommandError
|
||||
*/
|
||||
public function upload(string $src, string $dest): array
|
||||
{
|
||||
$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(),
|
||||
]);
|
||||
|
||||
$upload = $this->server->ssh()->exec($uploadCommand, 'upload-to-s3');
|
||||
|
||||
if (str_contains($upload, 'Error') || ! str_contains($upload, 'upload:')) {
|
||||
Log::error('Failed to upload to S3', ['output' => $upload]);
|
||||
throw new SSHCommandError('Failed to upload to S3: '.$upload);
|
||||
}
|
||||
|
||||
return [
|
||||
'size' => null, // You can parse the size from the output if needed
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHCommandError
|
||||
*/
|
||||
public function download(string $src, string $dest): void
|
||||
{
|
||||
$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(),
|
||||
]);
|
||||
|
||||
$download = $this->server->ssh()->exec($downloadCommand, 'download-from-s3');
|
||||
|
||||
if (! str_contains($download, 'Download successful')) {
|
||||
Log::error('Failed to download from S3', ['output' => $download]);
|
||||
throw new SSHCommandError('Failed to download from S3: '.$download);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO Implement delete method
|
||||
*/
|
||||
public function delete(string $path): void {}
|
||||
}
|
32
app/SSH/Storage/S3AbstractStorage.php
Normal file
32
app/SSH/Storage/S3AbstractStorage.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Storage;
|
||||
|
||||
abstract class S3AbstractStorage extends AbstractStorage
|
||||
{
|
||||
protected ?string $apiUrl = null;
|
||||
|
||||
protected ?string $bucketRegion = null;
|
||||
|
||||
public function getApiUrl(): string
|
||||
{
|
||||
return $this->apiUrl;
|
||||
}
|
||||
|
||||
public function setApiUrl(?string $region = null): void
|
||||
{
|
||||
$this->bucketRegion = $region ?? $this->bucketRegion;
|
||||
$this->apiUrl = "https://s3.{$this->bucketRegion}.amazonaws.com";
|
||||
}
|
||||
|
||||
// Getter and Setter for $bucketRegion
|
||||
public function getBucketRegion(): string
|
||||
{
|
||||
return $this->bucketRegion;
|
||||
}
|
||||
|
||||
public function setBucketRegion(string $region): void
|
||||
{
|
||||
$this->bucketRegion = $region;
|
||||
}
|
||||
}
|
84
app/SSH/Storage/Wasabi.php
Normal file
84
app/SSH/Storage/Wasabi.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\SSH\Storage;
|
||||
|
||||
use App\Exceptions\SSHCommandError;
|
||||
use App\Models\Server;
|
||||
use App\Models\StorageProvider;
|
||||
use App\SSH\HasS3Storage;
|
||||
use App\SSH\HasScripts;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Wasabi extends S3AbstractStorage
|
||||
{
|
||||
use HasS3Storage, HasScripts;
|
||||
|
||||
public function __construct(Server $server, StorageProvider $storageProvider)
|
||||
{
|
||||
parent::__construct($server, $storageProvider);
|
||||
$this->setBucketRegion($this->storageProvider->credentials['region']);
|
||||
$this->setApiUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHCommandError
|
||||
*/
|
||||
public function upload(string $src, string $dest): array
|
||||
{
|
||||
$uploadCommand = $this->getScript('wasabi/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->storageProvider->credentials['region'],
|
||||
'endpoint' => $this->getApiUrl(),
|
||||
]);
|
||||
|
||||
$upload = $this->server->ssh()->exec($uploadCommand, 'upload-to-wasabi');
|
||||
|
||||
if (str_contains($upload, 'Error') || ! str_contains($upload, 'upload:')) {
|
||||
Log::error('Failed to upload to wasabi', ['output' => $upload]);
|
||||
throw new SSHCommandError('Failed to upload to wasabi: '.$upload);
|
||||
}
|
||||
|
||||
return [
|
||||
'size' => null, // You can parse the size from the output if needed
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SSHCommandError
|
||||
*/
|
||||
public function download(string $src, string $dest): void
|
||||
{
|
||||
$downloadCommand = $this->getScript('wasabi/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->storageProvider->credentials['region'],
|
||||
'endpoint' => $this->getApiUrl(),
|
||||
]);
|
||||
|
||||
$download = $this->server->ssh()->exec($downloadCommand, 'download-from-wasabi');
|
||||
|
||||
if (! str_contains($download, 'Download successful')) {
|
||||
Log::error('Failed to download from wasabi', ['output' => $download]);
|
||||
throw new SSHCommandError('Failed to download from wasabi: '.$download);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO Implement delete method
|
||||
*/
|
||||
public function delete(string $path): void {}
|
||||
|
||||
public function setApiUrl(?string $region = null): void
|
||||
{
|
||||
$this->bucketRegion = $region ?? $this->bucketRegion;
|
||||
$this->apiUrl = "https://{$this->storageProvider->credentials['bucket']}.s3.{$this->getBucketRegion()}.wasabisys.com";
|
||||
}
|
||||
}
|
32
app/SSH/Storage/scripts/s3/download.sh
Normal file
32
app/SSH/Storage/scripts/s3/download.sh
Normal file
@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Configure AWS CLI with provided credentials
|
||||
/usr/local/bin/aws configure set aws_access_key_id "__key__"
|
||||
/usr/local/bin/aws configure set aws_secret_access_key "__secret__"
|
||||
/usr/local/bin/aws configure set default.region "__region__"
|
||||
|
||||
# Use the provided endpoint in the correct format
|
||||
ENDPOINT="__endpoint__"
|
||||
BUCKET="__bucket__"
|
||||
REGION="__region__"
|
||||
|
||||
# Ensure that DEST does not have a trailing slash
|
||||
SRC="__src__"
|
||||
DEST="__dest__"
|
||||
|
||||
# Download the file from S3
|
||||
echo "Downloading s3://__bucket__/__src__ to __dest__"
|
||||
download_output=$(/usr/local/bin/aws s3 cp "s3://$BUCKET/$SRC" "$DEST" --endpoint-url="$ENDPOINT" --region "$REGION" 2>&1)
|
||||
download_exit_code=$?
|
||||
|
||||
# Log output and exit code
|
||||
echo "Download command output: $download_output"
|
||||
echo "Download command exit code: $download_exit_code"
|
||||
|
||||
# Check if the download was successful
|
||||
if [ $download_exit_code -eq 0 ]; then
|
||||
echo "Download successful"
|
||||
else
|
||||
echo "Download failed"
|
||||
exit 1
|
||||
fi
|
59
app/SSH/Storage/scripts/s3/upload.sh
Normal file
59
app/SSH/Storage/scripts/s3/upload.sh
Normal file
@ -0,0 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check if AWS CLI is installed
|
||||
if ! command -v aws &> /dev/null
|
||||
then
|
||||
echo "AWS CLI is not installed. Installing..."
|
||||
|
||||
# Detect system architecture
|
||||
ARCH=$(uname -m)
|
||||
if [ "$ARCH" == "x86_64" ]; then
|
||||
CLI_URL="https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"
|
||||
elif [ "$ARCH" == "aarch64" ]; then
|
||||
CLI_URL="https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip"
|
||||
else
|
||||
echo "Unsupported architecture: $ARCH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Download and install AWS CLI
|
||||
sudo curl "$CLI_URL" -o "awscliv2.zip"
|
||||
sudo unzip awscliv2.zip
|
||||
sudo ./aws/install --update
|
||||
sudo rm -rf awscliv2.zip aws
|
||||
|
||||
echo "AWS CLI installation completed."
|
||||
else
|
||||
echo "AWS CLI is already installed."
|
||||
/usr/local/bin/aws --version
|
||||
fi
|
||||
|
||||
# Configure AWS CLI with provided credentials
|
||||
/usr/local/bin/aws configure set aws_access_key_id "__key__"
|
||||
/usr/local/bin/aws configure set aws_secret_access_key "__secret__"
|
||||
|
||||
# Use the provided endpoint in the correct format
|
||||
ENDPOINT="__endpoint__"
|
||||
BUCKET="__bucket__"
|
||||
REGION="__region__"
|
||||
|
||||
# Ensure that DEST does not have a trailing slash
|
||||
SRC="__src__"
|
||||
DEST="__dest__"
|
||||
|
||||
# Upload the file
|
||||
echo "Uploading __src__ to s3://$BUCKET/$DEST"
|
||||
upload_output=$(/usr/local/bin/aws s3 cp "$SRC" "s3://$BUCKET/$DEST" --endpoint-url="$ENDPOINT" --region "$REGION" 2>&1)
|
||||
upload_exit_code=$?
|
||||
|
||||
# Log output and exit code
|
||||
echo "Upload command output: $upload_output"
|
||||
echo "Upload command exit code: $upload_exit_code"
|
||||
|
||||
# Check if the upload was successful
|
||||
if [ $upload_exit_code -eq 0 ]; then
|
||||
echo "Upload successful"
|
||||
else
|
||||
echo "Upload failed"
|
||||
exit 1
|
||||
fi
|
31
app/SSH/Storage/scripts/wasabi/download.sh
Normal file
31
app/SSH/Storage/scripts/wasabi/download.sh
Normal file
@ -0,0 +1,31 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Configure AWS CLI with provided credentials
|
||||
/usr/local/bin/aws configure set aws_access_key_id "__key__"
|
||||
/usr/local/bin/aws configure set aws_secret_access_key "__secret__"
|
||||
|
||||
# Use the provided endpoint in the correct format
|
||||
ENDPOINT="__endpoint__"
|
||||
BUCKET="__bucket__"
|
||||
REGION="__region__"
|
||||
|
||||
# Ensure that DEST does not have a trailing slash
|
||||
SRC="__src__"
|
||||
DEST="__dest__"
|
||||
|
||||
# Download the file from S3
|
||||
echo "Downloading s3://__bucket____src__ to __dest__"
|
||||
download_output=$(/usr/local/bin/aws s3 cp "s3://$BUCKET/$SRC" "$DEST" --endpoint-url="$ENDPOINT" --region "$REGION" 2>&1)
|
||||
download_exit_code=$?
|
||||
|
||||
# Log output and exit code
|
||||
echo "Download command output: $download_output"
|
||||
echo "Download command exit code: $download_exit_code"
|
||||
|
||||
# Check if the download was successful
|
||||
if [ $download_exit_code -eq 0 ]; then
|
||||
echo "Download successful"
|
||||
else
|
||||
echo "Download failed"
|
||||
exit 1
|
||||
fi
|
59
app/SSH/Storage/scripts/wasabi/upload.sh
Normal file
59
app/SSH/Storage/scripts/wasabi/upload.sh
Normal file
@ -0,0 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Check if AWS CLI is installed
|
||||
if ! command -v aws &> /dev/null
|
||||
then
|
||||
echo "AWS CLI is not installed. Installing..."
|
||||
|
||||
# Detect system architecture
|
||||
ARCH=$(uname -m)
|
||||
if [ "$ARCH" == "x86_64" ]; then
|
||||
CLI_URL="https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"
|
||||
elif [ "$ARCH" == "aarch64" ]; then
|
||||
CLI_URL="https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip"
|
||||
else
|
||||
echo "Unsupported architecture: $ARCH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Download and install AWS CLI
|
||||
sudo curl "$CLI_URL" -o "awscliv2.zip"
|
||||
sudo unzip awscliv2.zip
|
||||
sudo ./aws/install --update
|
||||
sudo rm -rf awscliv2.zip aws
|
||||
|
||||
echo "AWS CLI installation completed."
|
||||
else
|
||||
echo "AWS CLI is already installed."
|
||||
aws --version
|
||||
fi
|
||||
|
||||
# Configure AWS CLI with provided credentials
|
||||
/usr/local/bin/aws configure set aws_access_key_id "__key__"
|
||||
/usr/local/bin/aws configure set aws_secret_access_key "__secret__"
|
||||
|
||||
# Use the provided endpoint in the correct format
|
||||
ENDPOINT="__endpoint__"
|
||||
BUCKET="__bucket__"
|
||||
REGION="__region__"
|
||||
|
||||
# Ensure that DEST does not have a trailing slash
|
||||
SRC="__src__"
|
||||
DEST="__dest__"
|
||||
|
||||
# Upload the file
|
||||
echo "Uploading __src__ to s3://$BUCKET/$DEST"
|
||||
upload_output=$(/usr/local/bin/aws s3 cp "$SRC" "s3://$BUCKET/$DEST" --endpoint-url="$ENDPOINT" --region "$REGION" 2>&1)
|
||||
upload_exit_code=$?
|
||||
|
||||
# Log output and exit code
|
||||
echo "Upload command output: $upload_output"
|
||||
echo "Upload command exit code: $upload_exit_code"
|
||||
|
||||
# Check if the upload was successful
|
||||
if [ $upload_exit_code -eq 0 ]; then
|
||||
echo "Upload successful"
|
||||
else
|
||||
echo "Upload failed"
|
||||
exit 1
|
||||
fi
|
57
app/StorageProviders/S3.php
Normal file
57
app/StorageProviders/S3.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\StorageProviders;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\SSH\Storage\S3 as S3Storage;
|
||||
use App\SSH\Storage\Storage;
|
||||
use Aws\S3\Exception\S3Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class S3 extends S3AbstractStorageProvider
|
||||
{
|
||||
public function validationRules(): array
|
||||
{
|
||||
return [
|
||||
'key' => 'required|string',
|
||||
'secret' => 'required|string',
|
||||
'region' => 'required|string',
|
||||
'bucket' => 'required|string',
|
||||
'path' => 'required|string',
|
||||
];
|
||||
}
|
||||
|
||||
public function credentialData(array $input): array
|
||||
{
|
||||
return [
|
||||
'key' => $input['key'],
|
||||
'secret' => $input['secret'],
|
||||
'region' => $input['region'],
|
||||
'bucket' => $input['bucket'],
|
||||
'path' => $input['path'],
|
||||
];
|
||||
}
|
||||
|
||||
public function connect(): bool
|
||||
{
|
||||
try {
|
||||
$this->setBucketRegion($this->storageProvider->credentials['region']);
|
||||
$this->setApiUrl();
|
||||
$this->buildClientConfig();
|
||||
$this->getClient()->listBuckets();
|
||||
|
||||
return true;
|
||||
} catch (S3Exception $e) {
|
||||
Log::error('Failed to connect to S3', ['exception' => $e]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function ssh(Server $server): Storage
|
||||
{
|
||||
return new S3Storage($server, $this->storageProvider);
|
||||
}
|
||||
|
||||
public function delete(array $paths): void {}
|
||||
}
|
74
app/StorageProviders/S3AbstractStorageProvider.php
Normal file
74
app/StorageProviders/S3AbstractStorageProvider.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\StorageProviders;
|
||||
|
||||
use App\Models\StorageProvider;
|
||||
use Aws\S3\S3Client;
|
||||
|
||||
abstract class S3AbstractStorageProvider extends AbstractStorageProvider implements S3ClientInterface, S3StorageInterface
|
||||
{
|
||||
protected ?string $apiUrl = null;
|
||||
|
||||
protected ?string $bucketRegion = null;
|
||||
|
||||
protected ?S3Client $client = null;
|
||||
|
||||
protected StorageProvider $storageProvider;
|
||||
|
||||
protected array $clientConfig = [];
|
||||
|
||||
public function getApiUrl(): string
|
||||
{
|
||||
return $this->apiUrl;
|
||||
}
|
||||
|
||||
public function setApiUrl(?string $region = null): void
|
||||
{
|
||||
$this->bucketRegion = $region ?? $this->bucketRegion;
|
||||
$this->apiUrl = "https://s3.{$this->bucketRegion}.amazonaws.com";
|
||||
}
|
||||
|
||||
public function getBucketRegion(): string
|
||||
{
|
||||
return $this->bucketRegion;
|
||||
}
|
||||
|
||||
public function setBucketRegion(string $region): void
|
||||
{
|
||||
$this->bucketRegion = $region;
|
||||
}
|
||||
|
||||
public function getClient(): S3Client
|
||||
{
|
||||
return new S3Client($this->clientConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the configuration array for the S3 client.
|
||||
* This method can be overridden by child classes to modify the configuration.
|
||||
*/
|
||||
public function buildClientConfig(): array
|
||||
{
|
||||
$this->clientConfig = [
|
||||
'credentials' => [
|
||||
'key' => $this->storageProvider->credentials['key'],
|
||||
'secret' => $this->storageProvider->credentials['secret'],
|
||||
],
|
||||
'region' => $this->getBucketRegion(),
|
||||
'version' => 'latest',
|
||||
'endpoint' => $this->getApiUrl(),
|
||||
];
|
||||
|
||||
return $this->clientConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or update a configuration parameter for the S3 client.
|
||||
*/
|
||||
public function setConfigParam(array $param): void
|
||||
{
|
||||
foreach ($param as $key => $value) {
|
||||
$this->clientConfig[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
10
app/StorageProviders/S3ClientInterface.php
Normal file
10
app/StorageProviders/S3ClientInterface.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\StorageProviders;
|
||||
|
||||
use Aws\S3\S3Client;
|
||||
|
||||
interface S3ClientInterface
|
||||
{
|
||||
public function getClient(): S3Client;
|
||||
}
|
14
app/StorageProviders/S3StorageInterface.php
Normal file
14
app/StorageProviders/S3StorageInterface.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\StorageProviders;
|
||||
|
||||
interface S3StorageInterface
|
||||
{
|
||||
public function getApiUrl(): string;
|
||||
|
||||
public function setApiUrl(?string $region = null): void;
|
||||
|
||||
public function getBucketRegion(): string;
|
||||
|
||||
public function setBucketRegion(string $region): void;
|
||||
}
|
85
app/StorageProviders/Wasabi.php
Normal file
85
app/StorageProviders/Wasabi.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\StorageProviders;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\SSH\Storage\Storage;
|
||||
use App\SSH\Storage\Wasabi as WasabiStorage;
|
||||
use Aws\S3\Exception\S3Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Wasabi extends S3AbstractStorageProvider
|
||||
{
|
||||
private const DEFAULT_REGION = 'us-east-1';
|
||||
|
||||
public function validationRules(): array
|
||||
{
|
||||
return [
|
||||
'key' => 'required|string',
|
||||
'secret' => 'required|string',
|
||||
'region' => 'required|string',
|
||||
'bucket' => 'required|string',
|
||||
'path' => 'required|string',
|
||||
];
|
||||
}
|
||||
|
||||
public function credentialData(array $input): array
|
||||
{
|
||||
return [
|
||||
'key' => $input['key'],
|
||||
'secret' => $input['secret'],
|
||||
'region' => $input['region'],
|
||||
'bucket' => $input['bucket'],
|
||||
'path' => $input['path'],
|
||||
];
|
||||
}
|
||||
|
||||
public function connect(): bool
|
||||
{
|
||||
try {
|
||||
$this->setBucketRegion(self::DEFAULT_REGION);
|
||||
$this->setApiUrl();
|
||||
$this->buildClientConfig();
|
||||
$this->getClient()->listBuckets();
|
||||
|
||||
return true;
|
||||
} catch (S3Exception $e) {
|
||||
Log::error('Failed to connect to S3', ['exception' => $e]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the configuration array for the S3 client.
|
||||
* This method can be overridden by child classes to modify the configuration.
|
||||
*/
|
||||
public function buildClientConfig(): array
|
||||
{
|
||||
$this->clientConfig = [
|
||||
'credentials' => [
|
||||
'key' => $this->storageProvider->credentials['key'],
|
||||
'secret' => $this->storageProvider->credentials['secret'],
|
||||
],
|
||||
'region' => $this->getBucketRegion(),
|
||||
'version' => 'latest',
|
||||
'endpoint' => $this->getApiUrl(),
|
||||
'use_path_style_endpoint' => true,
|
||||
];
|
||||
|
||||
return $this->clientConfig;
|
||||
}
|
||||
|
||||
public function ssh(Server $server): Storage
|
||||
{
|
||||
return new WasabiStorage($server, $this->storageProvider);
|
||||
}
|
||||
|
||||
public function setApiUrl(?string $region = null): void
|
||||
{
|
||||
$this->bucketRegion = $region ?? $this->bucketRegion;
|
||||
$this->apiUrl = "https://s3.{$this->bucketRegion}.wasabisys.com";
|
||||
}
|
||||
|
||||
public function delete(array $paths): void {}
|
||||
}
|
@ -427,11 +427,16 @@
|
||||
\App\Enums\StorageProvider::DROPBOX,
|
||||
\App\Enums\StorageProvider::FTP,
|
||||
\App\Enums\StorageProvider::LOCAL,
|
||||
\App\Enums\StorageProvider::S3,
|
||||
\App\Enums\StorageProvider::WASABI,
|
||||
],
|
||||
'storage_providers_class' => [
|
||||
\App\Enums\StorageProvider::DROPBOX => \App\StorageProviders\Dropbox::class,
|
||||
\App\Enums\StorageProvider::FTP => \App\StorageProviders\FTP::class,
|
||||
\App\Enums\StorageProvider::LOCAL => \App\StorageProviders\Local::class,
|
||||
\App\Enums\StorageProvider::S3 => \App\StorageProviders\S3::class,
|
||||
\App\Enums\StorageProvider::WASABI => \App\StorageProviders\Wasabi::class,
|
||||
|
||||
],
|
||||
|
||||
'ssl_types' => [
|
||||
|
1
public/static/images/s3.svg
Normal file
1
public/static/images/s3.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg width="256" height="256" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid"><title>AWS Simple Storage Service (S3)</title><defs><linearGradient x1="0%" y1="100%" x2="100%" y2="0%" id="a"><stop stop-color="#1B660F" offset="0%"/><stop stop-color="#6CAE3E" offset="100%"/></linearGradient></defs><g><rect fill="url(#a)" width="256" height="256"/><path d="M194.67488,137.25632 L195.90368,128.60352 C207.23488,135.39072 207.38208,138.19392 207.378964,138.27072 C207.35968,138.28672 205.42688,139.89952 194.67488,137.25632 L194.67488,137.25632 Z M188.45728,135.52832 C168.87328,129.60192 141.59968,117.08992 130.56288,111.87392 C130.56288,111.82912 130.57568,111.78752 130.57568,111.74272 C130.57568,107.50272 127.12608,104.05312 122.88288,104.05312 C118.64608,104.05312 115.19648,107.50272 115.19648,111.74272 C115.19648,115.98272 118.64608,119.43232 122.88288,119.43232 C124.74528,119.43232 126.43488,118.73792 127.76928,117.63392 C140.75488,123.78112 167.81728,136.11072 187.54528,141.93472 L179.74368,196.99392 C179.72128,197.14432 179.71168,197.29472 179.71168,197.44512 C179.71168,202.29312 158.24928,211.19872 123.18048,211.19872 C87.74048,211.19872 66.05088,202.29312 66.05088,197.44512 C66.05088,197.29792 66.04128,197.15392 66.02208,197.00992 L49.72128,77.94752 C63.83008,87.65952 94.17568,92.79872 123.19968,92.79872 C152.17888,92.79872 182.47328,87.67872 196.61088,77.99552 L188.45728,135.52832 Z M47.99968,65.52832 C48.23008,61.31712 72.42848,44.79872 123.19968,44.79872 C173.96448,44.79872 198.16608,61.31392 198.39968,65.52832 L198.39968,66.96512 C195.61568,76.40832 164.25568,86.39872 123.19968,86.39872 C82.07328,86.39872 50.69728,76.37632 47.99968,66.92032 L47.99968,65.52832 Z M204.79968,65.59872 C204.79968,54.51072 173.01088,38.39872 123.19968,38.39872 C73.38848,38.39872 41.59968,54.51072 41.59968,65.59872 L41.90048,68.01152 L59.65408,197.68832 C60.07968,212.19072 98.75488,217.59872 123.18048,217.59872 C153.49088,217.59872 185.69248,210.62912 186.10848,197.69792 L193.77568,143.62752 C198.04128,144.64832 201.55168,145.16992 204.37088,145.16992 C208.15648,145.16992 210.71648,144.24512 212.26848,142.39552 C213.54208,140.87872 214.02848,139.04192 213.66368,137.08672 C212.83488,132.65792 207.57728,127.88352 196.87008,121.77472 L204.47328,68.13632 L204.79968,65.59872 Z" fill="#FFFFFF"/></g></svg>
|
After Width: | Height: | Size: 2.3 KiB |
9
public/static/images/wasabi.svg
Normal file
9
public/static/images/wasabi.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 51 KiB |
@ -1,3 +1,14 @@
|
||||
@use(App\Enums\StorageProvider)
|
||||
@php
|
||||
$storageProviders = [
|
||||
StorageProvider::DROPBOX,
|
||||
StorageProvider::FTP,
|
||||
StorageProvider::LOCAL,
|
||||
StorageProvider::S3,
|
||||
StorageProvider::WASABI,
|
||||
];
|
||||
@endphp
|
||||
|
||||
<div>
|
||||
<x-primary-button x-data="" x-on:click.prevent="$dispatch('open-modal', 'connect-provider')">
|
||||
{{ __("Connect") }}
|
||||
@ -69,136 +80,8 @@ class="p-6"
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
@if ($provider == \App\Enums\StorageProvider::DROPBOX)
|
||||
<div class="mt-6">
|
||||
<x-input-label for="token" value="API Key" />
|
||||
<x-text-input value="{{ old('token') }}" id="token" name="token" type="text" class="mt-1 w-full" />
|
||||
@error("token")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
|
||||
<a
|
||||
class="mt-1 text-primary-500"
|
||||
href="https://dropbox.tech/developers/generate-an-access-token-for-your-own-account"
|
||||
target="_blank"
|
||||
>
|
||||
How to generate?
|
||||
</a>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@if ($provider == \App\Enums\StorageProvider::FTP)
|
||||
<div class="mt-6">
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="host" value="Host" />
|
||||
<x-text-input
|
||||
value="{{ old('host') }}"
|
||||
id="host"
|
||||
name="host"
|
||||
type="text"
|
||||
class="mt-1 w-full"
|
||||
/>
|
||||
@error("host")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="port" value="Port" />
|
||||
<x-text-input
|
||||
value="{{ old('port') }}"
|
||||
id="port"
|
||||
name="port"
|
||||
type="text"
|
||||
class="mt-1 w-full"
|
||||
/>
|
||||
@error("port")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="path" value="Path" />
|
||||
<x-text-input value="{{ old('path') }}" id="path" name="path" type="text" class="mt-1 w-full" />
|
||||
@error("path")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="username" value="Username" />
|
||||
<x-text-input
|
||||
value="{{ old('username') }}"
|
||||
id="username"
|
||||
name="username"
|
||||
type="text"
|
||||
class="mt-1 w-full"
|
||||
/>
|
||||
@error("username")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="password" value="Password" />
|
||||
<x-text-input
|
||||
value="{{ old('password') }}"
|
||||
id="password"
|
||||
name="password"
|
||||
type="text"
|
||||
class="mt-1 w-full"
|
||||
/>
|
||||
@error("password")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="ssl" :value="__('SSL')" />
|
||||
<x-select-input id="ssl" name="ssl" class="mt-1 w-full">
|
||||
<option value="1" @if(old('ssl')) selected @endif>
|
||||
{{ __("Yes") }}
|
||||
</option>
|
||||
<option value="0" @if(!old('ssl')) selected @endif>
|
||||
{{ __("No") }}
|
||||
</option>
|
||||
</x-select-input>
|
||||
@error("ssl")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="passive" :value="__('Passive')" />
|
||||
<x-select-input id="passive" name="passive" class="mt-1 w-full">
|
||||
<option value="1" @if(old('passive')) selected @endif>
|
||||
{{ __("Yes") }}
|
||||
</option>
|
||||
<option value="0" @if(!old('passive')) selected @endif>
|
||||
{{ __("No") }}
|
||||
</option>
|
||||
</x-select-input>
|
||||
@error("passive")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
@if ($provider == \App\Enums\StorageProvider::LOCAL)
|
||||
<div class="mt-6">
|
||||
<x-input-label for="path" value="Absolute Path" />
|
||||
<x-text-input value="{{ old('path') }}" id="path" name="path" type="text" class="mt-1 w-full" />
|
||||
<x-input-help>
|
||||
The absolute path on your server that the database exists. like `/home/vito/db-backups`
|
||||
</x-input-help>
|
||||
<x-input-help>
|
||||
Make sure that the path exists and the `vito` user has permission to write to it.
|
||||
</x-input-help>
|
||||
@error("path")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
@if (in_array($provider, $storageProviders))
|
||||
@include("settings.storage-providers.providers.{$provider}")
|
||||
@endif
|
||||
|
||||
<div class="mt-6">
|
||||
|
@ -0,0 +1,15 @@
|
||||
<div class="mt-6">
|
||||
<x-input-label for="token" value="API Key" />
|
||||
<x-text-input value="{{ old('token') }}" id="token" name="token" type="text" class="mt-1 w-full" />
|
||||
@error("token")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
|
||||
<a
|
||||
class="mt-1 text-primary-500"
|
||||
href="https://dropbox.tech/developers/generate-an-access-token-for-your-own-account"
|
||||
target="_blank"
|
||||
>
|
||||
How to generate?
|
||||
</a>
|
||||
</div>
|
@ -0,0 +1,71 @@
|
||||
<div class="mt-6">
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="host" value="Host" />
|
||||
<x-text-input value="{{ old('host') }}" id="host" name="host" type="text" class="mt-1 w-full" />
|
||||
@error("host")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="port" value="Port" />
|
||||
<x-text-input value="{{ old('port') }}" id="port" name="port" type="text" class="mt-1 w-full" />
|
||||
@error("port")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="path" value="Path" />
|
||||
<x-text-input value="{{ old('path') }}" id="path" name="path" type="text" class="mt-1 w-full" />
|
||||
@error("path")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="username" value="Username" />
|
||||
<x-text-input value="{{ old('username') }}" id="username" name="username" type="text" class="mt-1 w-full" />
|
||||
@error("username")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="password" value="Password" />
|
||||
<x-text-input value="{{ old('password') }}" id="password" name="password" type="text" class="mt-1 w-full" />
|
||||
@error("password")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="ssl" :value="__('SSL')" />
|
||||
<x-select-input id="ssl" name="ssl" class="mt-1 w-full">
|
||||
<option value="1" @if(old('ssl')) selected @endif>
|
||||
{{ __("Yes") }}
|
||||
</option>
|
||||
<option value="0" @if(!old('ssl')) selected @endif>
|
||||
{{ __("No") }}
|
||||
</option>
|
||||
</x-select-input>
|
||||
@error("ssl")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="passive" :value="__('Passive')" />
|
||||
<x-select-input id="passive" name="passive" class="mt-1 w-full">
|
||||
<option value="1" @if(old('passive')) selected @endif>
|
||||
{{ __("Yes") }}
|
||||
</option>
|
||||
<option value="0" @if(!old('passive')) selected @endif>
|
||||
{{ __("No") }}
|
||||
</option>
|
||||
</x-select-input>
|
||||
@error("passive")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@ -0,0 +1,9 @@
|
||||
<div class="mt-6">
|
||||
<x-input-label for="path" value="Absolute Path" />
|
||||
<x-text-input value="{{ old('path') }}" id="path" name="path" type="text" class="mt-1 w-full" />
|
||||
<x-input-help>The absolute path on your server that the database exists. like `/home/vito/db-backups`</x-input-help>
|
||||
<x-input-help>Make sure that the path exists and the `vito` user has permission to write to it.</x-input-help>
|
||||
@error("path")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
@ -0,0 +1,49 @@
|
||||
<div class="mt-6">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="path" value="Path" />
|
||||
<x-text-input value="{{ old('path') }}" id="path" name="path" type="text" class="mt-1 w-full" />
|
||||
@error("path")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="key" value="Key" />
|
||||
<x-text-input value="{{ old('key') }}" id="key" name="key" type="text" class="mt-1 w-full" />
|
||||
@error("key")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="secret" value="Secret" />
|
||||
<x-text-input value="{{ old('secret') }}" id="secret" name="secret" type="text" class="mt-1 w-full" />
|
||||
@error("secret")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<a
|
||||
class="mt-1 text-primary-500"
|
||||
href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/configuring-bucket-key.html"
|
||||
target="_blank"
|
||||
>
|
||||
How to generate?
|
||||
</a>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="region" value="Region" />
|
||||
<x-text-input value="{{ old('region') }}" id="region" name="region" type="text" class="mt-1 w-full" />
|
||||
@error("region")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="bucket" value="Bucket" />
|
||||
<x-text-input value="{{ old('bucket') }}" id="bucket" name="bucket" type="text" class="mt-1 w-full" />
|
||||
@error("bucket")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@ -0,0 +1,49 @@
|
||||
<div class="mt-6">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="path" value="Path" />
|
||||
<x-text-input value="{{ old('path') }}" id="path" name="path" type="text" class="mt-1 w-full" />
|
||||
@error("path")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="key" value="Key" />
|
||||
<x-text-input value="{{ old('key') }}" id="key" name="key" type="text" class="mt-1 w-full" />
|
||||
@error("key")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="secret" value="Secret" />
|
||||
<x-text-input value="{{ old('secret') }}" id="secret" name="secret" type="text" class="mt-1 w-full" />
|
||||
@error("secret")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
|
||||
<a
|
||||
class="mt-1 text-primary-500"
|
||||
href="https://docs.wasabi.com/docs/creating-a-user-account-and-access-key"
|
||||
target="_blank"
|
||||
>
|
||||
How to generate?
|
||||
</a>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="mt-6">
|
||||
<x-input-label for="region" value="Region" />
|
||||
<x-text-input value="{{ old('region') }}" id="region" name="region" type="text" class="mt-1 w-full" />
|
||||
@error("region")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<x-input-label for="bucket" value="Bucket" />
|
||||
<x-text-input value="{{ old('bucket') }}" id="bucket" name="bucket" type="text" class="mt-1 w-full" />
|
||||
@error("bucket")
|
||||
<x-input-error class="mt-2" :messages="$message" />
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
@ -6,6 +6,11 @@
|
||||
use App\Facades\FTP;
|
||||
use App\Models\Backup;
|
||||
use App\Models\Database;
|
||||
use App\Models\StorageProvider as StorageProviderModel;
|
||||
use App\StorageProviders\S3;
|
||||
use App\StorageProviders\Wasabi;
|
||||
use Aws\S3\Exception\S3Exception;
|
||||
use Aws\S3\S3Client;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
@ -52,6 +57,24 @@ public function test_see_providers_list(): void
|
||||
'provider' => StorageProvider::DROPBOX,
|
||||
]);
|
||||
|
||||
$this->get(route('settings.storage-providers'))
|
||||
->assertSuccessful()
|
||||
->assertSee($provider->profile);
|
||||
|
||||
$provider = \App\Models\StorageProvider::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'provider' => StorageProvider::S3,
|
||||
]);
|
||||
|
||||
$this->get(route('settings.storage-providers'))
|
||||
->assertSuccessful()
|
||||
->assertSee($provider->profile);
|
||||
|
||||
$provider = \App\Models\StorageProvider::factory()->create([
|
||||
'user_id' => $this->user->id,
|
||||
'provider' => StorageProvider::WASABI,
|
||||
]);
|
||||
|
||||
$this->get(route('settings.storage-providers'))
|
||||
->assertSuccessful()
|
||||
->assertSee($provider->profile);
|
||||
@ -101,6 +124,178 @@ public function test_cannot_delete_provider(): void
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_s3_connect_successful()
|
||||
{
|
||||
$storageProvider = StorageProviderModel::factory()->create([
|
||||
'provider' => StorageProvider::S3,
|
||||
'credentials' => [
|
||||
'key' => 'fake-key',
|
||||
'secret' => 'fake-secret',
|
||||
'region' => 'us-east-1',
|
||||
'bucket' => 'fake-bucket',
|
||||
'path' => '/',
|
||||
],
|
||||
]);
|
||||
|
||||
// Mock the S3Client
|
||||
$s3ClientMock = $this->getMockBuilder(S3Client::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getCommand', 'execute'])
|
||||
->getMock();
|
||||
|
||||
// Mock the getCommand method
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('getCommand')
|
||||
->with('listBuckets')
|
||||
->willReturn(new \Aws\Command('listBuckets'));
|
||||
|
||||
// Mock the execute method
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('execute')
|
||||
->willReturn(['Buckets' => []]);
|
||||
|
||||
// Mock the S3 class to return the mocked S3Client
|
||||
$s3 = $this->getMockBuilder(S3::class)
|
||||
->setConstructorArgs([$storageProvider])
|
||||
->onlyMethods(['getClient'])
|
||||
->getMock();
|
||||
|
||||
$s3->expects($this->once())
|
||||
->method('getClient')
|
||||
->willReturn($s3ClientMock);
|
||||
|
||||
$this->assertTrue($s3->connect());
|
||||
}
|
||||
|
||||
public function test_s3_connect_failure()
|
||||
{
|
||||
$storageProvider = StorageProviderModel::factory()->create([
|
||||
'provider' => StorageProvider::S3,
|
||||
'credentials' => [
|
||||
'key' => 'fake-key',
|
||||
'secret' => 'fake-secret',
|
||||
'region' => 'us-east-1',
|
||||
'bucket' => 'fake-bucket',
|
||||
'path' => '/',
|
||||
],
|
||||
]);
|
||||
|
||||
// Mock the S3Client
|
||||
$s3ClientMock = $this->getMockBuilder(S3Client::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getCommand', 'execute'])
|
||||
->getMock();
|
||||
|
||||
// Mock the getCommand method
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('getCommand')
|
||||
->with('listBuckets')
|
||||
->willReturn(new \Aws\Command('listBuckets'));
|
||||
|
||||
// Mock the execute method to throw an S3Exception
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('execute')
|
||||
->willThrowException(new S3Exception('Error', new \Aws\Command('ListBuckets')));
|
||||
|
||||
// Mock the S3 class to return the mocked S3Client
|
||||
$s3 = $this->getMockBuilder(S3::class)
|
||||
->setConstructorArgs([$storageProvider])
|
||||
->onlyMethods(['getClient'])
|
||||
->getMock();
|
||||
|
||||
$s3->expects($this->once())
|
||||
->method('getClient')
|
||||
->willReturn($s3ClientMock);
|
||||
|
||||
$this->assertFalse($s3->connect());
|
||||
}
|
||||
|
||||
public function test_wasabi_connect_successful()
|
||||
{
|
||||
$storageProvider = StorageProviderModel::factory()->create([
|
||||
'provider' => StorageProvider::WASABI,
|
||||
'credentials' => [
|
||||
'key' => 'fake-key',
|
||||
'secret' => 'fake-secret',
|
||||
'region' => 'us-east-1',
|
||||
'bucket' => 'fake-bucket',
|
||||
'path' => '/',
|
||||
],
|
||||
]);
|
||||
|
||||
// Mock the S3Client (Wasabi uses S3-compatible API)
|
||||
$s3ClientMock = $this->getMockBuilder(S3Client::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getCommand', 'execute'])
|
||||
->getMock();
|
||||
|
||||
// Mock the getCommand method
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('getCommand')
|
||||
->with('listBuckets')
|
||||
->willReturn(new \Aws\Command('listBuckets'));
|
||||
|
||||
// Mock the execute method
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('execute')
|
||||
->willReturn(['Buckets' => []]);
|
||||
|
||||
// Mock the Wasabi class to return the mocked S3Client
|
||||
$wasabi = $this->getMockBuilder(Wasabi::class)
|
||||
->setConstructorArgs([$storageProvider])
|
||||
->onlyMethods(['getClient'])
|
||||
->getMock();
|
||||
|
||||
$wasabi->expects($this->once())
|
||||
->method('getClient')
|
||||
->willReturn($s3ClientMock);
|
||||
|
||||
$this->assertTrue($wasabi->connect());
|
||||
}
|
||||
|
||||
public function test_wasabi_connect_failure()
|
||||
{
|
||||
$storageProvider = StorageProviderModel::factory()->create([
|
||||
'provider' => StorageProvider::WASABI,
|
||||
'credentials' => [
|
||||
'key' => 'fake-key',
|
||||
'secret' => 'fake-secret',
|
||||
'region' => 'us-east-1',
|
||||
'bucket' => 'fake-bucket',
|
||||
'path' => '/',
|
||||
],
|
||||
]);
|
||||
|
||||
// Mock the S3Client (Wasabi uses S3-compatible API)
|
||||
$s3ClientMock = $this->getMockBuilder(S3Client::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getCommand', 'execute'])
|
||||
->getMock();
|
||||
|
||||
// Mock the getCommand method
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('getCommand')
|
||||
->with('listBuckets')
|
||||
->willReturn(new \Aws\Command('listBuckets'));
|
||||
|
||||
// Mock the execute method to throw an S3Exception
|
||||
$s3ClientMock->expects($this->once())
|
||||
->method('execute')
|
||||
->willThrowException(new S3Exception('Error', new \Aws\Command('ListBuckets')));
|
||||
|
||||
// Mock the Wasabi class to return the mocked S3Client
|
||||
$wasabi = $this->getMockBuilder(Wasabi::class)
|
||||
->setConstructorArgs([$storageProvider])
|
||||
->onlyMethods(['getClient'])
|
||||
->getMock();
|
||||
|
||||
$wasabi->expects($this->once())
|
||||
->method('getClient')
|
||||
->willReturn($s3ClientMock);
|
||||
|
||||
$this->assertFalse($wasabi->connect());
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: complete FTP tests
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user