From 246fde0666ebd9a074998f59e284ff4ab297a105 Mon Sep 17 00:00:00 2001 From: Tushar Banik Date: Thu, 19 Oct 2023 15:19:27 +0530 Subject: [PATCH 1/5] added CloudfareR2 adaptar --- .github/workflows/tests.yml | 2 + docker-compose.yml | 4 +- src/Storage/Device/CloudflareR2.php | 70 ++++++++++++++++++++++++ src/Storage/Storage.php | 2 + tests/Storage/Device/CloudfareR2Test.php | 34 ++++++++++++ 5 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/Storage/Device/CloudflareR2.php create mode 100644 tests/Storage/Device/CloudfareR2Test.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 43d7ca7e..ee44cdc6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 }} + CLOUDFLARE_R2_SECRET: ${{ secrets.CLOUDFLARE_R2_SECRET }} run: | docker compose up -d sleep 10 diff --git a/docker-compose.yml b/docker-compose.yml index 4cd5e9f7..e3644ce1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,4 +20,6 @@ services: - BACKBLAZE_ACCESS_KEY - BACKBLAZE_SECRET - WASABI_ACCESS_KEY - - WASABI_SECRET \ No newline at end of file + - WASABI_SECRET + - CLOUDFLARE_R2_ACCESS_KEY + - CLOUDFLARE_R2_SECRET \ No newline at end of file diff --git a/src/Storage/Device/CloudflareR2.php b/src/Storage/Device/CloudflareR2.php new file mode 100644 index 00000000..1e93f49e --- /dev/null +++ b/src/Storage/Device/CloudflareR2.php @@ -0,0 +1,70 @@ +headers['host'] = $bucket.'.'.'s3'.'.'.$region.'.cloudflarestorage.com'; + } + + /** + * @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; + } +} diff --git a/src/Storage/Storage.php b/src/Storage/Storage.php index cabd18c8..8ec344d1 100644 --- a/src/Storage/Storage.php +++ b/src/Storage/Storage.php @@ -21,6 +21,8 @@ class Storage const DEVICE_LINODE = 'linode'; + const CLOUDFARE_R2 = 'cloudflare-r2'; + /** * Devices. * diff --git a/tests/Storage/Device/CloudfareR2Test.php b/tests/Storage/Device/CloudfareR2Test.php new file mode 100644 index 00000000..40cee655 --- /dev/null +++ b/tests/Storage/Device/CloudfareR2Test.php @@ -0,0 +1,34 @@ +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'; + } +} From 9014289d51d326622fe318216dded880bd0e7771 Mon Sep 17 00:00:00 2001 From: Tushar Banik Date: Fri, 20 Oct 2023 00:11:11 +0530 Subject: [PATCH 2/5] lint tests --- src/Storage/Device/CloudflareR2.php | 25 +++++++++++------------- tests/Storage/Device/CloudfareR2Test.php | 2 +- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Storage/Device/CloudflareR2.php b/src/Storage/Device/CloudflareR2.php index 1e93f49e..0d9215f6 100644 --- a/src/Storage/Device/CloudflareR2.php +++ b/src/Storage/Device/CloudflareR2.php @@ -6,26 +6,24 @@ class CloudflareR2 extends S3 { - /** * Regions constants */ - - /** + + /** * Regions constants */ - - const WNAM = 'us-west-1'; + const WNAM = 'us-west-1'; + + const ENAM = 'us-east-1'; + + const WEUR = 'eu-west-1'; + + const EEUR = 'eu-east-1'; - const ENAM = 'us-east-1'; - - const WEUR = 'eu-west-1'; - - const EEUR = 'eu-east-1'; - - const APAC = 'ap-southeast-1'; + const APAC = 'ap-southeast-1'; - const AUTO = 'auto'; + const AUTO = 'auto'; /** * CloudflareR2 Constructor @@ -37,7 +35,6 @@ class CloudflareR2 extends S3 * @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); diff --git a/tests/Storage/Device/CloudfareR2Test.php b/tests/Storage/Device/CloudfareR2Test.php index 40cee655..b9723306 100644 --- a/tests/Storage/Device/CloudfareR2Test.php +++ b/tests/Storage/Device/CloudfareR2Test.php @@ -14,7 +14,7 @@ protected function init(): void $secret = $_SERVER['CLOUDFLARE_R2_SECRET'] ?? ''; $bucket = 'utopia-storage-test'; - $this->object = new CloudflareR2($this->root, $key, $secret, $bucket, CloudflareR2:: AUTO , CloudflareR2::ACL_PRIVATE); + $this->object = new CloudflareR2($this->root, $key, $secret, $bucket, CloudflareR2::AUTO, CloudflareR2::ACL_PRIVATE); } protected function getAdapterName(): string From b1944abf74ce0e3f969da91184e02c3c2cedcecb Mon Sep 17 00:00:00 2001 From: Tushar Banik Date: Wed, 25 Oct 2023 12:34:20 +0530 Subject: [PATCH 3/5] minor changes --- .github/workflows/tests.yml | 2 +- src/Storage/Device/CloudflareR2.php | 2 +- src/Storage/Storage.php | 2 +- .../Device/{CloudfareR2Test.php => CloudflareR2Test.php} | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename tests/Storage/Device/{CloudfareR2Test.php => CloudflareR2Test.php} (95%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index ee44cdc6..7451af9a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/src/Storage/Device/CloudflareR2.php b/src/Storage/Device/CloudflareR2.php index 0d9215f6..430ac1d0 100644 --- a/src/Storage/Device/CloudflareR2.php +++ b/src/Storage/Device/CloudflareR2.php @@ -26,7 +26,7 @@ class CloudflareR2 extends S3 const AUTO = 'auto'; /** - * CloudflareR2 Constructor + * Cloudflare R2 Constructor * * @param string $root * @param string $accessKey diff --git a/src/Storage/Storage.php b/src/Storage/Storage.php index 8ec344d1..fe759d2f 100644 --- a/src/Storage/Storage.php +++ b/src/Storage/Storage.php @@ -21,7 +21,7 @@ class Storage const DEVICE_LINODE = 'linode'; - const CLOUDFARE_R2 = 'cloudflare-r2'; + const CLOUDFARE_R2 = 'cloudflarer2'; /** * Devices. diff --git a/tests/Storage/Device/CloudfareR2Test.php b/tests/Storage/Device/CloudflareR2Test.php similarity index 95% rename from tests/Storage/Device/CloudfareR2Test.php rename to tests/Storage/Device/CloudflareR2Test.php index b9723306..c19f1db5 100644 --- a/tests/Storage/Device/CloudfareR2Test.php +++ b/tests/Storage/Device/CloudflareR2Test.php @@ -5,7 +5,7 @@ use Utopia\Storage\Device\CloudflareR2; use Utopia\Tests\Storage\S3Base; -class CloudfareR2Test extends S3Base +class CloudflareR2Test extends S3Base { protected function init(): void { From fd7939b968cc137ed5cee49dfdeb3f6b21bc4f99 Mon Sep 17 00:00:00 2001 From: Tushar Banik <107763774+Tushar98644@users.noreply.github.com> Date: Tue, 31 Oct 2023 21:40:42 +0530 Subject: [PATCH 4/5] Update CloudflareR2.php --- src/Storage/Device/CloudflareR2.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Storage/Device/CloudflareR2.php b/src/Storage/Device/CloudflareR2.php index 430ac1d0..8d7c9f19 100644 --- a/src/Storage/Device/CloudflareR2.php +++ b/src/Storage/Device/CloudflareR2.php @@ -8,6 +8,7 @@ class CloudflareR2 extends S3 { /** * Regions constants + * Reference: https://developers.cloudflare.com/durable-objects/platform/data-location/ */ /** From 13c7cdb5a3b0a49e1d9aad17cf10c1dff7b3e7fb Mon Sep 17 00:00:00 2001 From: Tushar Banik <107763774+Tushar98644@users.noreply.github.com> Date: Tue, 31 Oct 2023 21:40:56 +0530 Subject: [PATCH 5/5] Update .github/workflows/tests.yml Co-authored-by: Steven Nguyen <1477010+stnguyen90@users.noreply.github.com> --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7451af9a..0da274c0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -58,7 +58,7 @@ jobs: strategy: fail-fast: false matrix: - devices: [BackblazeTest, DOSpacesTest, LinodeTest, LocalTest, S3Test, WasabiTest , CloudflareR2Test] + devices: [BackblazeTest, DOSpacesTest, LinodeTest, LocalTest, S3Test, WasabiTest, CloudflareR2Test] steps: - name: checkout