-
Notifications
You must be signed in to change notification settings - Fork 63
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
} | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
} | ||
} |
There was a problem hiding this comment.
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.