Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: S3Compatible storage adapter #76

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
S3 compatible storage
  • Loading branch information
haymaker committed Jun 5, 2023
commit 42f78d4311a72ffe3dd13bb6b7c9ab2f4441039a
84 changes: 84 additions & 0 deletions src/Storage/Device/S3Compatible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Utopia\Storage\Device;

use function preg_replace;
haymaker marked this conversation as resolved.
Show resolved Hide resolved
use Utopia\Storage\Storage;

/**
* Adapter for S3-Compatible storage providers
*
* Endpoint URL is explicitly defined by the caller due to providers allowing
* path-style or vhost-style endpoints as well as extra parameters such as
* client ID.
*/
class S3Compatible extends S3
{
/**
* @var string
*/
protected string $endpoint;

/**
* S3Compatible Constructor
*
* @param string $endpoint
* @param string $root
* @param string $accessKey
* @param string $secretKey
* @param string $bucket
* @param bool $vhost
* @param string $region
* @param string $acl
*/
public function __construct(string $endpoint, string $root, string $accessKey, string $secretKey, string $bucket, bool $vhost, string $region = self::US_EAST_1, string $acl = self::ACL_PRIVATE)
{
if (! $vhost) {
$root = $bucket.$root;
}
parent::__construct($root, $accessKey, $secretKey, $bucket, $region, $acl);

$this->endpoint = $endpoint;
$this->vhost = $vhost;

/**
* Workaround to prevent having to do a refactor of
* the S3 class along with adapter addition. Class only
* supports https for the time being.
*
* There are multiple endpoint styles dependent upon the provider
* used. Examples are:
* <scheme>:https://<endpoint.url>/<bucket>
* <scheme>:https://<clientid.endpoint.url>/<bucket>
* <scheme>:https://<bucketvhost.endpoint.url>
*/
$endpoint = preg_replace('/^https?:\/\//i', '', $endpoint);
$this->headers['host'] = $endpoint;
}

/**
* @return string
*/
public function getName(): string
{
return 'S3-Compatible Storage';
}

/**
* @return string
*/
public function getType(): string
{
return Storage::DEVICE_S3COMPATIBLE;
}

/**
* @return string
*/
public function getDescription(): string
{
return 'Generic Connector For S3-Compatible Storage Providers';
}
}
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_S3COMPATIBLE = 's3compatible';

/**
* Devices.
*
Expand Down