Skip to content

Commit

Permalink
Added support for Alibaba Cloud storage adapter
Browse files Browse the repository at this point in the history
Signed-off-by: nidhi-singh02 <nidhi2894@gmail.com>
  • Loading branch information
nidhi-singh02 committed Oct 14, 2023
1 parent efec537 commit a3a956c
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
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');
}

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);
$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);
$this->alibaba->exists('path');
}

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

0 comments on commit a3a956c

Please sign in to comment.