-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlobServiceClient.php
More file actions
222 lines (195 loc) · 8.6 KB
/
Copy pathBlobServiceClient.php
File metadata and controls
222 lines (195 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace AzureOss\Storage\Blob;
use AzureOss\Identity\TokenCredential;
use AzureOss\Storage\Blob\Exceptions\BlobStorageExceptionDeserializer;
use AzureOss\Storage\Blob\Exceptions\InvalidConnectionStringException;
use AzureOss\Storage\Blob\Exceptions\UnableToGenerateSasException;
use AzureOss\Storage\Blob\Models\BlobContainer;
use AzureOss\Storage\Blob\Models\BlobContainerClientOptions;
use AzureOss\Storage\Blob\Models\BlobContainerInclude;
use AzureOss\Storage\Blob\Models\BlobServiceClientOptions;
use AzureOss\Storage\Blob\Models\GetBlobContainersOptions;
use AzureOss\Storage\Blob\Models\TaggedBlob;
use AzureOss\Storage\Blob\Responses\FindBlobsByTagBody;
use AzureOss\Storage\Blob\Responses\ListContainersResponseBody;
use AzureOss\Storage\Common\Auth\StorageSharedKeyCredential;
use AzureOss\Storage\Common\Helpers\ConnectionStringHelper;
use AzureOss\Storage\Common\Helpers\StorageUriParserHelper;
use AzureOss\Storage\Common\Middleware\ClientFactory;
use AzureOss\Storage\Common\Sas\AccountSasBuilder;
use AzureOss\Storage\Common\Sas\AccountSasServices;
use AzureOss\Storage\Common\Sas\SasProtocol;
use GuzzleHttp\Client;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\UriInterface;
/**
* Provides service-level operations for an Azure Blob Storage account.
*
* Create this client from a service endpoint and credential, or use
* {@see self::fromConnectionString()} when a storage connection string is available.
*/
final class BlobServiceClient
{
private readonly Client $client;
/**
* @param UriInterface $uri Blob service endpoint, including any SAS query string.
* @param StorageSharedKeyCredential|TokenCredential|null $credential Credential used to authorize requests, or null for anonymous/SAS access.
* @param BlobServiceClientOptions $options Client transport and service-version options.
*/
public function __construct(
public UriInterface $uri,
public readonly StorageSharedKeyCredential|TokenCredential|null $credential = null,
private readonly BlobServiceClientOptions $options = new BlobServiceClientOptions,
) {
// must always include the forward slash (/) to separate the host name from the path and query portions of the URI.
$this->uri = $uri->withPath(rtrim($uri->getPath(), '/').'/');
$this->client = (new ClientFactory)->create($this->uri, $credential, new BlobStorageExceptionDeserializer, $this->options->httpClientOptions, $this->options->apiVersion);
}
/**
* Creates a client from an Azure Storage connection string.
*
* @throws InvalidConnectionStringException When the connection string does not contain a usable Blob endpoint and credential.
*/
public static function fromConnectionString(string $connectionString, BlobServiceClientOptions $options = new BlobServiceClientOptions): self
{
$uri = ConnectionStringHelper::getBlobEndpoint($connectionString);
if ($uri === null) {
throw new InvalidConnectionStringException;
}
$sas = ConnectionStringHelper::getSas($connectionString);
if ($sas !== null) {
return new self($uri->withQuery($sas), options: $options);
}
$accountName = ConnectionStringHelper::getAccountName($connectionString);
$accountKey = ConnectionStringHelper::getAccountKey($connectionString);
if ($accountName !== null && $accountKey !== null) {
return new self($uri, new StorageSharedKeyCredential($accountName, $accountKey), $options);
}
throw new InvalidConnectionStringException;
}
/**
* Creates a client for the named blob container without making a service request.
*/
public function getContainerClient(string $containerName): BlobContainerClient
{
return new BlobContainerClient(
$this->uri->withPath($this->uri->getPath().$containerName),
$this->credential,
new BlobContainerClientOptions($this->options->httpClientOptions, $this->options->apiVersion),
);
}
/**
* Lists containers in the storage account, following continuation markers automatically.
*
* @param string|null $prefix Optional prefix used to filter container names.
* @return \Generator<BlobContainer>
*/
public function getBlobContainers(?string $prefix = null, GetBlobContainersOptions $options = new GetBlobContainersOptions): \Generator
{
$nextMarker = '';
while (true) {
$response = $this->client->get($this->uri, [
RequestOptions::QUERY => [
'comp' => 'list',
'marker' => $nextMarker !== '' ? $nextMarker : null,
'prefix' => $prefix,
'maxresults' => $options->pageSize,
'include' => $options->includes !== []
? implode(',', array_map(static fn (BlobContainerInclude $include): string => $include->value, $options->includes))
: null,
],
]);
$body = ListContainersResponseBody::fromXml(new \SimpleXMLElement($response->getBody()->getContents()));
$nextMarker = $body->nextMarker;
foreach ($body->containers as $container) {
yield $container;
}
if ($nextMarker === '') {
break;
}
}
}
/**
* Restores a soft-deleted container identified by its name and deleted version.
*
* The version is returned by {@see self::getBlobContainers()} when
* {@see BlobContainerInclude::DELETED} is requested.
*/
public function undeleteBlobContainer(string $deletedContainerName, string $deletedContainerVersion): BlobContainerClient
{
return $this->undeleteBlobContainerAsync($deletedContainerName, $deletedContainerVersion)->wait();
}
/**
* Asynchronously restores a soft-deleted container and returns a client for it.
*
* @return PromiseInterface<BlobContainerClient, mixed>
*/
public function undeleteBlobContainerAsync(string $deletedContainerName, string $deletedContainerVersion): PromiseInterface
{
$container = $this->getContainerClient($deletedContainerName);
return $this->client->putAsync($container->uri, [
RequestOptions::QUERY => [
'restype' => 'container',
'comp' => 'undelete',
],
RequestOptions::HEADERS => [
'x-ms-deleted-container-name' => $deletedContainerName,
'x-ms-deleted-container-version' => $deletedContainerVersion,
],
])->then(fn () => $container);
}
/**
* Finds blobs across the storage account whose index tags match the SQL expression.
*
* @return \Generator<TaggedBlob>
*/
public function findBlobsByTag(string $tagFilterSqlExpression): \Generator
{
$nextMarker = '';
while (true) {
$response = $this->client->get($this->uri, [
RequestOptions::QUERY => [
'comp' => 'blobs',
'where' => $tagFilterSqlExpression,
'marker' => $nextMarker !== '' ? $nextMarker : null,
],
]);
$body = FindBlobsByTagBody::fromXml(new \SimpleXMLElement($response->getBody()->getContents()));
$nextMarker = $body->nextMarker;
foreach ($body->blobs as $blob) {
yield $blob;
}
if ($nextMarker === '') {
break;
}
}
}
/**
* Returns whether this client has a shared-key credential capable of signing an account SAS.
*/
public function canGenerateAccountSasUri(): bool
{
return $this->credential instanceof StorageSharedKeyCredential;
}
/**
* Generates a Blob service URI containing a signed account SAS query string.
*
* @throws UnableToGenerateSasException When the client does not have a shared-key credential.
*/
public function generateAccountSasUri(AccountSasBuilder $accountSasBuilder): UriInterface
{
if (! $this->credential instanceof StorageSharedKeyCredential) {
throw new UnableToGenerateSasException;
}
if (StorageUriParserHelper::isDevelopmentUri($this->uri)) {
$accountSasBuilder->setProtocol(SasProtocol::HTTPS_AND_HTTP);
}
$sas = $accountSasBuilder
->setServices(new AccountSasServices(blob: true))
->build($this->credential);
return new Uri("$this->uri?$sas");
}
}