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

Added support for CloudflareR2 Storage adaptar #97

Closed
wants to merge 5 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
strategy:
fail-fast: false
matrix:
devices: [BackblazeTest, DOSpacesTest, LinodeTest, LocalTest, S3Test, WasabiTest]
devices: [BackblazeTest, DOSpacesTest, LinodeTest, LocalTest, S3Test, WasabiTest, CloudflareR2Test]

steps:
- name: checkout
Expand All @@ -75,6 +75,8 @@ jobs:
WASABI_SECRET: ${{ secrets.WASABI_SECRET }}
BACKBLAZE_ACCESS_KEY: ${{ secrets.BACKBLAZE_ACCESS_KEY }}
BACKBLAZE_SECRET: ${{ secrets.BACKBLAZE_SECRET }}
CLOUDFLARE_R2_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY }}
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved
CLOUDFLARE_R2_SECRET: ${{ secrets.CLOUDFLARE_R2_SECRET }}
run: |
docker compose up -d
sleep 10
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ services:
- BACKBLAZE_ACCESS_KEY
- BACKBLAZE_SECRET
- WASABI_ACCESS_KEY
- WASABI_SECRET
- WASABI_SECRET
- CLOUDFLARE_R2_ACCESS_KEY
- CLOUDFLARE_R2_SECRET
68 changes: 68 additions & 0 deletions src/Storage/Device/CloudflareR2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Utopia\Storage\Device;

use Utopia\Storage\Storage;

class CloudflareR2 extends S3
{
/**
* Regions constants
* Reference: https://developers.cloudflare.com/durable-objects/platform/data-location/
*/

/**
* Regions constants
*/
Comment on lines +9 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consolidate

const WNAM = 'us-west-1';

const ENAM = 'us-east-1';

const WEUR = 'eu-west-1';

const EEUR = 'eu-east-1';

const APAC = 'ap-southeast-1';

const AUTO = 'auto';
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Cloudflare R2 Constructor
*
* @param string $root
* @param string $accessKey
* @param string $secretKey
* @param string $bucket
* @param string $region
* @param string $acl
*/
public function __construct(string $root, string $accessKey, string $secretKey, string $bucket, string $region = self::APAC, string $acl = self::ACL_PRIVATE)
{
parent::__construct($root, $accessKey, $secretKey, $bucket, $region, $acl);
$this->headers['host'] = $bucket.'.'.'s3'.'.'.$region.'.cloudflarestorage.com';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any documentation on the host?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An S3 API URL looks like this:
https://accountid.r2.cloudflarestorage.com/bucketname

Where account id is your CF Account ID, like this:
https://905fa3fe223dcff4bf6b46911342bcfd.r2.cloudflarestorage.com/test

If you are using the EU Juristiction, you need to add .eu before the .r2, like this:
https://905fa3fe223dcff4bf6b46911342bcfd.eu.r2.cloudflarestorage.com/test

}

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

/**
* @return string
*/
public function getDescription(): string
{
return 'Cloudflare R2 Storage';
}

/**
* @return string
*/
public function getType(): string
{
return Storage::DEVICE_CLOUDFLARER2;
}
}
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 CLOUDFARE_R2 = 'cloudflarer2';

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

namespace Utopia\Tests\Storage\Device;

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

class CloudflareR2Test extends S3Base
{
protected function init(): void
{
$this->root = '/root';
$key = $_SERVER['CLOUDFLARE_R2_ACCESS_KEY'] ?? '';
$secret = $_SERVER['CLOUDFLARE_R2_SECRET'] ?? '';
$bucket = 'utopia-storage-test';

$this->object = new CloudflareR2($this->root, $key, $secret, $bucket, CloudflareR2::AUTO, CloudflareR2::ACL_PRIVATE);
}

protected function getAdapterName(): string
{
return 'Cloudflare R2 Storage';
}

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

protected function getAdapterDescription(): string
{
return 'Cloudflare R2 Storage';
}
}