Skip to content

Commit a3a956c

Browse files
n0s09bynidhi-singh02
authored andcommitted
Added support for Alibaba Cloud storage adapter
Signed-off-by: nidhi-singh02 <nidhi2894@gmail.com>
1 parent efec537 commit a3a956c

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
namespace Utopia\Storage\Device;
4+
5+
use Utopia\Storage\Device;
6+
7+
class AlibabaCloud extends Device
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected string $accessKey;
13+
14+
/**
15+
* @var string
16+
*/
17+
protected string $secretKey;
18+
19+
/**
20+
* @var string
21+
*/
22+
protected string $bucket;
23+
24+
/**
25+
* @var string
26+
*/
27+
protected string $endpoint;
28+
29+
/**
30+
* @var OssClient
31+
*/
32+
protected OssClient $client;
33+
34+
/**
35+
* Alibaba constructor
36+
*
37+
* @param string $accessKey
38+
* @param string $secretKey
39+
* @param string $bucket
40+
* @param string $endpoint
41+
*/
42+
public function __construct(string $accessKey, string $secretKey, string $bucket, string $endpoint)
43+
{
44+
$this->accessKey = $accessKey;
45+
$this->secretKey = $secretKey;
46+
$this->bucket = $bucket;
47+
$this->endpoint = $endpoint;
48+
49+
try {
50+
$this->client = new OssClient($this->accessKey, $this->secretKey, $this->endpoint);
51+
} catch (OssException $e) {
52+
throw new Exception('Could not establish connection with Alibaba Cloud: '.$e->getMessage());
53+
}
54+
}
55+
56+
/**
57+
* @return string
58+
*/
59+
public function getName(): string
60+
{
61+
return 'Alibaba Cloud Storage';
62+
}
63+
64+
/**
65+
* @return string
66+
*/
67+
public function getType(): string
68+
{
69+
return Storage::DEVICE_ALIBABA_CLOUD;
70+
}
71+
72+
// Refer to the Alibaba Cloud OSS PHP SDK documentation for more details: https://www.alibabacloud.com/help/doc-detail/32099.htm
73+
74+
/**
75+
* @param string $path
76+
* @return string
77+
*/
78+
public function read(string $path): string
79+
{
80+
return $this->client->getObject($this->bucket, $path);
81+
}
82+
83+
/**
84+
* @param string $path
85+
* @param string $data
86+
* @return bool
87+
*/
88+
public function write(string $path, string $data): bool
89+
{
90+
try {
91+
$this->client->putObject($this->bucket, $path, $data);
92+
return true;
93+
} catch (OssException $e) {
94+
throw new Exception('Could not write data to Alibaba Cloud: '.$e->getMessage());
95+
}
96+
}
97+
98+
/**
99+
* @param string $path
100+
* @return bool
101+
*/
102+
public function delete(string $path): bool
103+
{
104+
try {
105+
$this->client->deleteObject($this->bucket, $path);
106+
return true;
107+
} catch (OssException $e) {
108+
throw new Exception('Could not delete data from Alibaba Cloud: '.$e->getMessage());
109+
}
110+
}
111+
112+
/**
113+
* @param string $path
114+
* @return bool
115+
*/
116+
public function exists(string $path): bool
117+
{
118+
return $this->client->doesObjectExist($this->bucket, $path);
119+
}
120+
121+
/**
122+
* @param string $path
123+
* @param string $filepath
124+
* @return bool
125+
*/
126+
public function upload(string $path, string $filepath): bool
127+
{
128+
try {
129+
$this->client->uploadFile($this->bucket, $path, $filepath);
130+
return true;
131+
} catch (OssException $e) {
132+
throw new Exception('Could not upload file to Alibaba Cloud: '.$e->getMessage());
133+
}
134+
}
135+
}

src/Storage/Storage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Storage
2121

2222
const DEVICE_LINODE = 'linode';
2323

24+
const DEVICE_ALIBABA_CLOUD = 'alibabacloud';
25+
2426
/**
2527
* Devices.
2628
*
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Utopia\Tests\Storage\Device;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use OSS\Core\OssException;
7+
use Utopia\Storage\Device\Alibaba;
8+
9+
class AlibabaCloudTest extends TestCase
10+
{
11+
private $alibaba;
12+
13+
protected function setUp(): void
14+
{
15+
$this->alibaba = new Alibaba('accessKey', 'secretKey', 'bucket', 'endpoint');
16+
}
17+
18+
public function testGetName()
19+
{
20+
$this->assertEquals('Alibaba Cloud Storage', $this->alibaba->getName());
21+
}
22+
23+
public function testGetType()
24+
{
25+
$this->assertEquals(Storage::DEVICE_ALIBABA_CLOUD, $this->alibaba->getType());
26+
}
27+
28+
public function testRead()
29+
{
30+
$this->expectException(OssException::class);
31+
$this->alibaba->read('path');
32+
}
33+
34+
public function testWrite()
35+
{
36+
$this->expectException(OssException::class);
37+
$this->alibaba->write('path', 'data');
38+
}
39+
40+
public function testDelete()
41+
{
42+
$this->expectException(OssException::class);
43+
$this->alibaba->delete('path');
44+
}
45+
46+
public function testExists()
47+
{
48+
$this->expectException(OssException::class);
49+
$this->alibaba->exists('path');
50+
}
51+
52+
public function testUpload()
53+
{
54+
$this->expectException(OssException::class);
55+
$this->alibaba->upload('path', 'filepath');
56+
}
57+
}

0 commit comments

Comments
 (0)