diff --git a/src/Storage/Device/AlibabaCloud.php b/src/Storage/Device/AlibabaCloud.php new file mode 100644 index 00000000..0015cabe --- /dev/null +++ b/src/Storage/Device/AlibabaCloud.php @@ -0,0 +1,135 @@ +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()); + } + } +} diff --git a/src/Storage/Storage.php b/src/Storage/Storage.php index cabd18c8..d4a83138 100644 --- a/src/Storage/Storage.php +++ b/src/Storage/Storage.php @@ -21,6 +21,8 @@ class Storage const DEVICE_LINODE = 'linode'; + const DEVICE_ALIBABA_CLOUD = 'alibabacloud'; + /** * Devices. * diff --git a/tests/Storage/Device/AlibabaCloudTest.php b/tests/Storage/Device/AlibabaCloudTest.php new file mode 100644 index 00000000..17736ab0 --- /dev/null +++ b/tests/Storage/Device/AlibabaCloudTest.php @@ -0,0 +1,57 @@ +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'); + } +}