Skip to content

Commit

Permalink
added CloudfareR2 adaptar
Browse files Browse the repository at this point in the history
  • Loading branch information
Tushar98644 committed Oct 19, 2023
1 parent e1d4f22 commit 246fde0
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
70 changes: 70 additions & 0 deletions src/Storage/Device/CloudflareR2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Utopia\Storage\Device;

use Utopia\Storage\Storage;

class CloudflareR2 extends S3
{

/**
* Regions constants
*/

/**
* Regions constants
*/

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';

/**
* CloudflareR2 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';
}

/**
* @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 = 'cloudflare-r2';

/**
* Devices.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Storage/Device/CloudfareR2Test.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 CloudfareR2Test 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';
}
}

0 comments on commit 246fde0

Please sign in to comment.