Passed
Push — master ( e165dc...217243 )
by Roeland
12:21 queued 10s
created

Azure::writeObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
4
 *
5
 * @author Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Files\ObjectStore;
25
26
use MicrosoftAzure\Storage\Blob\BlobRestProxy;
27
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;
28
use OCP\Files\ObjectStore\IObjectStore;
29
30
class Azure implements IObjectStore {
31
	/** @var string */
32
	private $containerName;
33
	/** @var string */
34
	private $accountName;
35
	/** @var string */
36
	private $accountKey;
37
	/** @var BlobRestProxy|null */
38
	private $blobClient = null;
39
	/** @var string|null */
40
	private $endpoint = null;
41
	/** @var bool  */
42
	private $autoCreate = false;
43
44
	/**
45
	 * @param array $parameters
46
	 */
47
	public function __construct($parameters) {
48
		$this->containerName = $parameters['container'];
49
		$this->accountName = $parameters['account_name'];
50
		$this->accountKey = $parameters['account_key'];
51
		if (isset($parameters['endpoint'])) {
52
			$this->endpoint = $parameters['endpoint'];
53
		}
54
		if (isset($parameters['autocreate'])) {
55
			$this->autoCreate = $parameters['autocreate'];
56
		}
57
	}
58
59
	/**
60
	 * @return BlobRestProxy
61
	 */
62
	private function getBlobClient() {
63
		if (!$this->blobClient) {
64
			$protocol = $this->endpoint ? substr($this->endpoint, 0, strpos($this->endpoint, ':')) : 'https';
65
			$connectionString = "DefaultEndpointsProtocol=" . $protocol . ";AccountName=" . $this->accountName . ";AccountKey=" . $this->accountKey;
66
			if ($this->endpoint) {
67
				$connectionString .= ';BlobEndpoint=' . $this->endpoint;
68
			}
69
			$this->blobClient = BlobRestProxy::createBlobService($connectionString);
70
71
			if ($this->autoCreate) {
72
				try {
73
					$this->blobClient->createContainer($this->containerName);
74
				} catch (ServiceException $e) {
75
					if ($e->getCode() === 409) {
76
						// already exists
77
					} else {
78
						throw $e;
79
					}
80
				}
81
			}
82
		}
83
		return $this->blobClient;
84
	}
85
86
	/**
87
	 * @return string the container or bucket name where objects are stored
88
	 */
89
	public function getStorageId() {
90
		return 'azure::blob::' . $this->containerName;
91
	}
92
93
	/**
94
	 * @param string $urn the unified resource name used to identify the object
95
	 * @return resource stream with the read data
96
	 * @throws \Exception when something goes wrong, message will be logged
97
	 */
98
	public function readObject($urn) {
99
		$blob = $this->getBlobClient()->getBlob($this->containerName, $urn);
100
		return $blob->getContentStream();
101
	}
102
103
	/**
104
	 * @param string $urn the unified resource name used to identify the object
105
	 * @param resource $stream stream with the data to write
106
	 * @throws \Exception when something goes wrong, message will be logged
107
	 */
108
	public function writeObject($urn, $stream) {
109
		$this->getBlobClient()->createBlockBlob($this->containerName, $urn, $stream);
110
	}
111
112
	/**
113
	 * @param string $urn the unified resource name used to identify the object
114
	 * @return void
115
	 * @throws \Exception when something goes wrong, message will be logged
116
	 */
117
	public function deleteObject($urn) {
118
		$this->getBlobClient()->deleteBlob($this->containerName, $urn);
119
	}
120
121
	public function objectExists($urn) {
122
		try {
123
			$this->getBlobClient()->getBlobMetadata($this->containerName, $urn);
124
			return true;
125
		} catch (ServiceException $e) {
126
			if ($e->getCode() === 404) {
127
				return false;
128
			} else {
129
				throw $e;
130
			}
131
		}
132
	}
133
134
	public function copyObject($from, $to) {
135
		$this->getBlobClient()->copyBlob($this->containerName, $to, $this->containerName, $from);
136
	}
137
}
138