|
| 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 | +} |
0 commit comments