-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Alibaba Cloud storage adapter
Signed-off-by: nidhi-singh02 <nidhi2894@gmail.com>
- Loading branch information
1 parent
efec537
commit a3a956c
Showing
3 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |