-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBlobServiceClient.php
More file actions
161 lines (135 loc) · 5.6 KB
/
BlobServiceClient.php
File metadata and controls
161 lines (135 loc) · 5.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
<?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\Helpers\BlobUriParserHelper;
use AzureOss\Storage\Blob\Models\BlobContainer;
use AzureOss\Storage\Blob\Models\BlobContainerClientOptions;
use AzureOss\Storage\Blob\Models\BlobServiceClientOptions;
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\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\Psr7\Uri;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\UriInterface;
final class BlobServiceClient
{
private readonly Client $client;
/**
* @deprecated Use $credential instead.
*/
public ?StorageSharedKeyCredential $sharedKeyCredentials = null;
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);
if ($credential instanceof StorageSharedKeyCredential) {
/** @phpstan-ignore-next-line */
$this->sharedKeyCredentials = $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;
}
public function getContainerClient(string $containerName): BlobContainerClient
{
return new BlobContainerClient(
$this->uri->withPath($this->uri->getPath().$containerName),
$this->credential,
new BlobContainerClientOptions($this->options->httpClientOptions),
);
}
/**
* @return \Generator<BlobContainer>
*/
public function getBlobContainers(?string $prefix = null): \Generator
{
$nextMarker = '';
while (true) {
$response = $this->client->get($this->uri, [
RequestOptions::QUERY => [
'comp' => 'list',
'marker' => $nextMarker !== '' ? $nextMarker : null,
'prefix' => $prefix,
],
]);
$body = ListContainersResponseBody::fromXml(new \SimpleXMLElement($response->getBody()->getContents()));
$nextMarker = $body->nextMarker;
foreach ($body->containers as $container) {
yield $container;
}
if ($nextMarker === '') {
break;
}
}
}
/**
* @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;
}
}
}
public function canGenerateAccountSasUri(): bool
{
return $this->credential instanceof StorageSharedKeyCredential;
}
public function generateAccountSasUri(AccountSasBuilder $accountSasBuilder): UriInterface
{
if (! $this->credential instanceof StorageSharedKeyCredential) {
throw new UnableToGenerateSasException;
}
if (BlobUriParserHelper::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");
}
}