Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/Storage/Device/MinIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Utopia\Storage\Device;

class MinIO extends S3
{
/**
* Regions constants
*/
const EU_CENTRAL_1 = 'eu-central-1';

const US_SOUTHEAST_1 = 'us-southeast-1';

const US_EAST_1 = 'us-east-1';

const AP_SOUTH_1 = 'ap-south-1';

/**
* Object Storage Constructor
*
* @param string $root
* @param string $accessKey
* @param string $secretKey
* @param string $protocol
* @param string $host
* @param string $bucket
* @param string $region
* @param string $acl
*/
public function __construct(string $root, string $accessKey, string $secretKey, string $protocol, string $host, string $bucket, string $region = self::EU_CENTRAL_1, string $acl = self::ACL_PRIVATE)
{
parent::__construct($root, $accessKey, $secretKey, $bucket, $region, $acl);

$this->protocol = $protocol;
$this->headers['host'] = $host;
}

/**
* Get list of objects in the given path.
*
* @param string $path
* @return array
*
* @throws \Exception
*/
private function listObjects($prefix = '', $maxKeys = 1000, $continuationToken = '')
{
$uri = '/'.$this->getRoot();
$this->headers['content-type'] = 'text/plain';
$this->headers['content-md5'] = \base64_encode(md5('', true));

$parameters = [
'list-type' => 2,
'prefix' => $prefix,
'max-keys' => $maxKeys,
];
if (! empty($continuationToken)) {
$parameters['continuation-token'] = $continuationToken;
}
$response = parent::call(self::METHOD_GET, $uri, '', $parameters);

return $response->body;
}

/**
* Delete files in given path, path must be a directory. Return true on success and false on failure.
*
* @param string $path
* @return bool
*
* @throws \Exception
*/
public function deletePath(string $path): bool
{
$uri = '/'.$this->getRoot();
$continuationToken = '';
do {
$objects = $this->listObjects($path, continuationToken: $continuationToken);
$count = (int) ($objects['KeyCount'] ?? 1);
if ($count < 1) {
break;
}
$continuationToken = $objects['NextContinuationToken'] ?? '';
$body = '<Delete xmlns="http://s3.amazonaws.com/doc/2006-03-01/">';

if ($count > 1) {
foreach ($objects['Contents'] as $object) {
$body .= "<Object><Key>{$object['Key']}</Key></Object>";
}
} else {
$body .= "<Object><Key>{$objects['Contents']['Key']}</Key></Object>";
}
$body .= '<Quiet>true</Quiet>';
$body .= '</Delete>';
$this->amzHeaders['x-amz-content-sha256'] = \hash('sha256', $body);
$this->headers['content-md5'] = \base64_encode(md5($body, true));
parent::call(self::METHOD_POST, $uri, $body, ['delete' => '']);
} while (! empty($continuationToken));

return true;
}

/**
* @return string
*/
public function getName(): string
{
return 'MinIO Object Storage';
}

/**
* @return string
*/
public function getDescription(): string
{
return 'MinIO Object Storage';
}
}
9 changes: 7 additions & 2 deletions src/Storage/Device/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class S3 extends Device
*/
protected array $amzHeaders;

/**
* @var string
*/
protected string $protocol = 'https';

/**
* S3 Constructor
*
Expand Down Expand Up @@ -724,10 +729,10 @@ private function getSignatureV4(string $method, string $uri, array $parameters =
*
* @throws \Exception
*/
private function call(string $method, string $uri, string $data = '', array $parameters = [])
protected function call(string $method, string $uri, string $data = '', array $parameters = [])
{
$uri = $this->getAbsolutePath($uri);
$url = 'https://'.$this->headers['host'].$uri.'?'.\http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
$url = $this->protocol.'://'.$this->headers['host'].$uri.'?'.\http_build_query($parameters, '', '&', PHP_QUERY_RFC3986);
$response = new \stdClass;
$response->body = '';
$response->headers = [];
Expand Down
2 changes: 2 additions & 0 deletions src/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Storage

const DEVICE_LINODE = 'linode';

const DEVICE_MINIO = 'minio';

/**
* Devices.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/Storage/Device/MinIOTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Utopia\Tests;

use Utopia\Storage\Device\MinIO;
use Utopia\Tests\Storage\S3Base;

class MinIOTest extends S3Base
{
protected function init(): void
{
$this->root = 'minio-test-bucket';
$key = $_SERVER['MINIO_ACCESS_KEY'] ?? '';
$secret = $_SERVER['MINIO_SECRET'] ?? '';
$protocol = $_SERVER['MINIO_PROTOCOL'] ?? '';
$host = $_SERVER['MINIO_HOST'] ?? '';
$region = $_SERVER['MINIO_REGION'] ?? '';
$bucket = 'minio-test-bucket';

$this->object = new MinIO($this->root, $key, $secret, $protocol, $host, $bucket, $region);
}

protected function getAdapterName(): string
{
return 'MinIO Object Storage';
}

protected function getAdapterType(): string
{
return $this->object->getType();
}

protected function getAdapterDescription(): string
{
return 'MinIO Object Storage';
}
}