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 Alibaba Cloud storage adapter #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
135 changes: 135 additions & 0 deletions src/Storage/Device/AlibabaCloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Utopia\Storage\Device;

use Utopia\Storage\Device;

class AlibabaCloud extends Device
{
/**
* @var string
*/
protected string $accessKey;

/**
* @var string
*/
protected string $secretKey;

/**
* @var string
*/
protected string $bucket;

/**
* @var string
*/
protected string $endpoint;

/**
* @var OssClient
*/
protected OssClient $client;

/**
* Alibaba constructor
*
* @param string $accessKey
* @param string $secretKey
* @param string $bucket
* @param string $endpoint
*/
public function __construct(string $accessKey, string $secretKey, string $bucket, string $endpoint)
{
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->bucket = $bucket;
$this->endpoint = $endpoint;

try {
$this->client = new OssClient($this->accessKey, $this->secretKey, $this->endpoint);
} catch (OssException $e) {
throw new Exception('Could not establish connection with Alibaba Cloud: '.$e->getMessage());
}
}

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

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

// Refer to the Alibaba Cloud OSS PHP SDK documentation for more details: https://www.alibabacloud.com/help/doc-detail/32099.htm

/**
* @param string $path
* @return string
*/
public function read(string $path): string
{
return $this->client->getObject($this->bucket, $path);
}

/**
* @param string $path
* @param string $data
* @return bool
*/
public function write(string $path, string $data): bool
{
try {
$this->client->putObject($this->bucket, $path, $data);
return true;
} catch (OssException $e) {
throw new Exception('Could not write data to Alibaba Cloud: '.$e->getMessage());
}
}

/**
* @param string $path
* @return bool
*/
public function delete(string $path): bool
{
try {
$this->client->deleteObject($this->bucket, $path);
return true;
} catch (OssException $e) {
throw new Exception('Could not delete data from Alibaba Cloud: '.$e->getMessage());
}
}

/**
* @param string $path
* @return bool
*/
public function exists(string $path): bool
{
return $this->client->doesObjectExist($this->bucket, $path);
}

/**
* @param string $path
* @param string $filepath
* @return bool
*/
public function upload(string $path, string $filepath): bool
{
try {
$this->client->uploadFile($this->bucket, $path, $filepath);
return true;
} catch (OssException $e) {
throw new Exception('Could not upload file to Alibaba Cloud: '.$e->getMessage());
}
}
}
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_ALIBABA_CLOUD = 'alibabacloud';

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

namespace Utopia\Tests\Storage\Device;

use PHPUnit\Framework\TestCase;
use OSS\Core\OssException;
use Utopia\Storage\Device\Alibaba;

class AlibabaCloudTest extends TestCase
{
private $alibaba;

protected function setUp(): void
{
$this->alibaba = new Alibaba('accessKey', 'secretKey', 'bucket', 'endpoint');
Copy link
Member

Choose a reason for hiding this comment

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

You need to pass actual values of accessKey, secretKey etc. Those values can either be added to environment variables and read from there or you can take a reference from the S3 adapter.

}

public function testGetName()
{
$this->assertEquals('Alibaba Cloud Storage', $this->alibaba->getName());
}

public function testGetType()
{
$this->assertEquals(Storage::DEVICE_ALIBABA_CLOUD, $this->alibaba->getType());
}

public function testRead()
{
$this->expectException(OssException::class);
Copy link
Member

Choose a reason for hiding this comment

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

The tests need to ensure that the method works as expected. You can use a test Alibaba Cloud account, read values from there and assert that the value is being read properly. Similarly for other tests.

$this->alibaba->read('path');
}

public function testWrite()
{
$this->expectException(OssException::class);
$this->alibaba->write('path', 'data');
}

public function testDelete()
{
$this->expectException(OssException::class);
$this->alibaba->delete('path');
}

public function testExists()
{
$this->expectException(OssException::class);
Copy link
Member

Choose a reason for hiding this comment

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

Add tests for all cases like when the value exists and when it doesn't exist.

$this->alibaba->exists('path');
}

public function testUpload()
{
$this->expectException(OssException::class);
$this->alibaba->upload('path', 'filepath');
}
}
Loading