/
BlobServiceClient.cs
2419 lines (2300 loc) · 107 KB
/
BlobServiceClient.cs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Cryptography;
using Azure.Storage.Sas;
using Metadata = System.Collections.Generic.IDictionary<string, string>;
namespace Azure.Storage.Blobs
{
/// <summary>
/// The <see cref="BlobServiceClient"/> allows you to manipulate Azure
/// Storage service resources and blob containers. The storage account provides
/// the top-level namespace for the Blob service.
/// </summary>
public class BlobServiceClient
{
/// <summary>
/// Gets the blob service's primary <see cref="Uri"/> endpoint.
/// </summary>
private readonly Uri _uri;
/// <summary>
/// Gets the blob service's primary <see cref="Uri"/> endpoint.
/// </summary>
public virtual Uri Uri => _uri;
/// <summary>
/// <see cref="BlobClientConfiguration"/>.
/// </summary>
private readonly BlobClientConfiguration _clientConfiguration;
/// <summary>
/// <see cref="BlobClientConfiguration"/>.
/// </summary>
internal virtual BlobClientConfiguration ClientConfiguration => _clientConfiguration;
/// <summary>
/// The authentication policy for our ClientConfiguration.Pipeline. We cache it here in
/// case we need to construct a pipeline for authenticating batch
/// operations.
/// </summary>
private readonly HttpPipelinePolicy _authenticationPolicy;
internal virtual HttpPipelinePolicy AuthenticationPolicy => _authenticationPolicy;
/// <summary>
/// The <see cref="ClientSideEncryptionOptions"/> to be used when sending/receiving requests.
/// </summary>
private readonly ClientSideEncryptionOptions _clientSideEncryption;
/// <summary>
/// The <see cref="ClientSideEncryptionOptions"/> to be used when sending/receiving requests.
/// </summary>
internal virtual ClientSideEncryptionOptions ClientSideEncryption => _clientSideEncryption;
/// <summary>
/// The Storage account name corresponding to the service client.
/// </summary>
private string _accountName;
/// <summary>
/// Gets the Storage account name corresponding to the service client.
/// </summary>
public string AccountName
{
get
{
if (_accountName == null)
{
_accountName = new BlobUriBuilder(Uri).AccountName;
}
return _accountName;
}
}
/// <summary>
/// Determines whether the client is able to generate a SAS.
/// If the client is authenticated with a <see cref="StorageSharedKeyCredential"/>.
/// </summary>
public virtual bool CanGenerateAccountSasUri => ClientConfiguration.SharedKeyCredential != null;
/// <summary>
/// <see cref="ServiceRestClient"/>.
/// </summary>
private readonly ServiceRestClient _serviceRestClient;
/// <summary>
/// <see cref="ServiceRestClient"/>.
/// </summary>
internal virtual ServiceRestClient ServiceRestClient => _serviceRestClient;
#region ctors
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class for mocking.
/// </summary>
protected BlobServiceClient()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="connectionString">
/// A connection string includes the authentication information
/// required for your application to access data in an Azure Storage
/// account at runtime.
///
/// For more information, <see href="https://docs.microsoft.com/azure/storage/common/storage-configure-connection-string">Configure Azure Storage connection strings</see>.
/// </param>
public BlobServiceClient(string connectionString)
: this(connectionString, null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="connectionString">
/// A connection string includes the authentication information
/// required for your application to access data in an Azure Storage
/// account at runtime.
///
/// For more information,
/// <see href="https://docs.microsoft.com/azure/storage/common/storage-configure-connection-string">
/// Configure Azure Storage connection strings</see>.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
public BlobServiceClient(string connectionString, BlobClientOptions options)
{
var conn = StorageConnectionString.Parse(connectionString);
_uri = conn.BlobEndpoint;
options ??= new BlobClientOptions();
_authenticationPolicy = StorageClientOptions.GetAuthenticationPolicy(conn.Credentials);
_clientConfiguration = new BlobClientConfiguration(
pipeline: options.Build(_authenticationPolicy),
sharedKeyCredential: conn.Credentials as StorageSharedKeyCredential,
clientDiagnostics: new ClientDiagnostics(options),
version: options.Version,
customerProvidedKey: options.CustomerProvidedKey,
transferValidation: options.TransferValidation,
encryptionScope: options.EncryptionScope,
trimBlobNameSlashes: options.TrimBlobNameSlashes);
_clientSideEncryption = options._clientSideEncryptionOptions?.Clone();
_serviceRestClient = BuildServiceRestClient(_uri);
BlobErrors.VerifyHttpsCustomerProvidedKey(_uri, _clientConfiguration.CustomerProvidedKey);
BlobErrors.VerifyCpkAndEncryptionScopeNotBothSet(_clientConfiguration.CustomerProvidedKey, _clientConfiguration.EncryptionScope);
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
public BlobServiceClient(Uri serviceUri, BlobClientOptions options = default)
: this(serviceUri, (HttpPipelinePolicy)null, options ?? new BlobClientOptions())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="credential">
/// The shared key credential used to sign requests.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
public BlobServiceClient(Uri serviceUri, StorageSharedKeyCredential credential, BlobClientOptions options = default)
: this(serviceUri, credential.AsPolicy(), credential, options ?? new BlobClientOptions())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// Must not contain shared access signature, which should be passed in the second parameter.
/// </param>
/// <param name="credential">
/// The shared access signature credential used to sign requests.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
/// <remarks>
/// This constructor should only be used when shared access signature needs to be updated during lifespan of this client.
/// </remarks>
public BlobServiceClient(Uri serviceUri, AzureSasCredential credential, BlobClientOptions options = default)
: this(serviceUri, credential.AsPolicy<BlobUriBuilder>(serviceUri), credential, options ?? new BlobClientOptions())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="credential">
/// The token credential used to sign requests.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
public BlobServiceClient(Uri serviceUri, TokenCredential credential, BlobClientOptions options = default)
: this(
serviceUri,
credential.AsPolicy(
string.IsNullOrEmpty(options?.Audience?.ToString()) ? BlobAudience.DefaultAudience.CreateDefaultScope() : options.Audience.Value.CreateDefaultScope(),
options),
credential,
options ?? new BlobClientOptions())
{
Errors.VerifyHttpsTokenAuth(serviceUri);
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="authentication">
/// An optional authentication policy used to sign requests.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
internal BlobServiceClient(
Uri serviceUri,
HttpPipelinePolicy authentication,
BlobClientOptions options)
: this(serviceUri,
new BlobClientConfiguration(
pipeline: options.Build(authentication),
sharedKeyCredential: default,
clientDiagnostics: new ClientDiagnostics(options),
version: options?.Version ?? BlobClientOptions.LatestVersion,
customerProvidedKey: options?.CustomerProvidedKey,
transferValidation: options.TransferValidation,
encryptionScope: options?.EncryptionScope,
trimBlobNameSlashes: options?.TrimBlobNameSlashes ?? false),
authentication,
options?._clientSideEncryptionOptions?.Clone())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="authentication">
/// An optional authentication policy used to sign requests.
/// </param>
/// <param name="storageSharedKeyCredential">
/// Optional storage shared key credential used to sign requests and generate sas.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
internal BlobServiceClient(
Uri serviceUri,
HttpPipelinePolicy authentication,
StorageSharedKeyCredential storageSharedKeyCredential,
BlobClientOptions options)
: this(serviceUri,
new BlobClientConfiguration(
pipeline: options.Build(authentication),
sharedKeyCredential: storageSharedKeyCredential,
clientDiagnostics: new ClientDiagnostics(options),
version: options?.Version ?? BlobClientOptions.LatestVersion,
customerProvidedKey: options?.CustomerProvidedKey,
transferValidation: options.TransferValidation,
encryptionScope: options?.EncryptionScope,
trimBlobNameSlashes: options?.TrimBlobNameSlashes ?? false),
authentication,
options?._clientSideEncryptionOptions?.Clone())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="authentication">
/// An optional authentication policy used to sign requests.
/// </param>
/// <param name="tokenCredential">
/// The token credential used to sign requests.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
internal BlobServiceClient(
Uri serviceUri,
HttpPipelinePolicy authentication,
TokenCredential tokenCredential,
BlobClientOptions options)
: this(serviceUri,
new BlobClientConfiguration(
pipeline: options.Build(authentication),
tokenCredential: tokenCredential,
clientDiagnostics: new ClientDiagnostics(options),
version: options?.Version ?? BlobClientOptions.LatestVersion,
customerProvidedKey: options?.CustomerProvidedKey,
transferValidation: options.TransferValidation,
encryptionScope: options?.EncryptionScope,
trimBlobNameSlashes: options?.TrimBlobNameSlashes ?? false),
authentication,
options?._clientSideEncryptionOptions?.Clone())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="authentication">
/// An optional authentication policy used to sign requests.
/// </param>
/// <param name="sasCredential">
/// Optional SAS credential used to sign requests and generate sas.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
internal BlobServiceClient(
Uri serviceUri,
HttpPipelinePolicy authentication,
AzureSasCredential sasCredential,
BlobClientOptions options)
: this(serviceUri,
new BlobClientConfiguration(
pipeline: options.Build(authentication),
sasCredential: sasCredential,
clientDiagnostics: new ClientDiagnostics(options),
version: options?.Version ?? BlobClientOptions.LatestVersion,
customerProvidedKey: options?.CustomerProvidedKey,
transferValidation: options.TransferValidation,
encryptionScope: options?.EncryptionScope,
trimBlobNameSlashes: options?.TrimBlobNameSlashes ?? false),
authentication,
options?._clientSideEncryptionOptions?.Clone())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the blob service.
/// This is likely to be similar to "https://{account_name}.blob.core.windows.net".
/// </param>
/// <param name="clientConfiguration">
/// <see cref="BlobClientConfiguration"/>.
/// </param>
/// <param name="authentication">
/// An optional authentication policy used to sign requests.
/// </param>
/// <param name="clientSideEncryption">
/// Client-side encryption options.
/// </param>
internal BlobServiceClient(
Uri serviceUri,
BlobClientConfiguration clientConfiguration,
HttpPipelinePolicy authentication,
ClientSideEncryptionOptions clientSideEncryption)
{
Argument.AssertNotNull(serviceUri, nameof(serviceUri));
_uri = serviceUri;
_clientConfiguration = clientConfiguration;
_authenticationPolicy = authentication;
_clientSideEncryption = clientSideEncryption?.Clone();
_serviceRestClient = BuildServiceRestClient(serviceUri);
BlobErrors.VerifyCpkAndEncryptionScopeNotBothSet(_clientConfiguration.CustomerProvidedKey, _clientConfiguration.EncryptionScope);
BlobErrors.VerifyHttpsCustomerProvidedKey(_uri, _clientConfiguration.CustomerProvidedKey);
}
/// <summary>
/// Intended for DataLake to create a backing blob client.
///
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the block blob that includes the
/// name of the account, the name of the container, and the name of
/// the blob.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
/// <param name="authentication">
/// An optional authentication policy used to sign requests.
/// </param>
/// <param name="pipeline">
/// The transport pipeline used to send every request.
/// </param>
/// <param name="sharedKeyCredential">
/// The shared key credential used to sign requests.
/// </param>
/// <param name="sasCredential">
/// The SAS credential used to sign requests.
/// </param>
/// <param name="tokenCredential">
/// The token credential used to sign requests.
/// </param>
/// <returns>
/// New instanc of the <see cref="BlobServiceClient"/> class.
/// </returns>
protected static BlobServiceClient CreateClient(
Uri serviceUri,
BlobClientOptions options,
HttpPipelinePolicy authentication,
HttpPipeline pipeline,
StorageSharedKeyCredential sharedKeyCredential,
AzureSasCredential sasCredential,
TokenCredential tokenCredential)
{
return new BlobServiceClient(
serviceUri,
new BlobClientConfiguration(
pipeline: pipeline,
sharedKeyCredential: sharedKeyCredential,
sasCredential: sasCredential,
tokenCredential: tokenCredential,
clientDiagnostics: new ClientDiagnostics(options),
version: options.Version,
customerProvidedKey: null,
transferValidation: options.TransferValidation,
encryptionScope: null,
trimBlobNameSlashes: options.TrimBlobNameSlashes),
authentication,
clientSideEncryption: null);
}
/// <summary>
/// Intended for DataLake to create a backing blob client.
///
/// Initializes a new instance of the <see cref="BlobServiceClient"/>
/// class.
/// </summary>
/// <param name="serviceUri">
/// A <see cref="Uri"/> referencing the block blob that includes the
/// name of the account, the name of the container, and the name of
/// the blob.
/// </param>
/// <param name="options">
/// Optional client options that define the transport pipeline
/// policies for authentication, retries, etc., that are applied to
/// every request.
/// </param>
/// <param name="authentication">
/// An optional authentication policy used to sign requests.
/// </param>
/// <param name="pipeline">
/// The transport pipeline used to send every request.
/// </param>
/// <returns>
/// New instanc of the <see cref="BlobServiceClient"/> class.
/// </returns>
[EditorBrowsable(EditorBrowsableState.Never)]
protected static BlobServiceClient CreateClient(
Uri serviceUri,
BlobClientOptions options,
HttpPipelinePolicy authentication,
HttpPipeline pipeline)
{
return new BlobServiceClient(
serviceUri,
new BlobClientConfiguration(
pipeline: pipeline,
sharedKeyCredential: null,
clientDiagnostics: new ClientDiagnostics(options),
version: options.Version,
customerProvidedKey: null,
transferValidation: options.TransferValidation,
encryptionScope: null,
trimBlobNameSlashes: options.TrimBlobNameSlashes),
authentication,
clientSideEncryption: null);
}
private ServiceRestClient BuildServiceRestClient(Uri uri)
=> new ServiceRestClient(
clientDiagnostics: _clientConfiguration.ClientDiagnostics,
pipeline: _clientConfiguration.Pipeline,
url: uri.AbsoluteUri,
version: _clientConfiguration.Version.ToVersionString());
#endregion ctors
/// <summary>
/// Create a new <see cref="BlobContainerClient"/> object by appending
/// <paramref name="blobContainerName"/> to the end of <see cref="Uri"/>.
/// The new <see cref="BlobContainerClient"/> uses the same request
/// policy pipeline as the <see cref="BlobServiceClient"/>.
/// </summary>
/// <param name="blobContainerName">
/// The name of the blob container to reference.
/// </param>
/// <returns>
/// A <see cref="BlobContainerClient"/> for the desired container.
/// </returns>
public virtual BlobContainerClient GetBlobContainerClient(string blobContainerName) =>
new BlobContainerClient(
Uri.AppendToPath(blobContainerName),
ClientConfiguration,
ClientSideEncryption);
#region protected static accessors for Azure.Storage.Blobs.Batch
/// <summary>
/// Get a <see cref="BlobServiceClient"/>'s <see cref="HttpPipeline"/>
/// for creating child clients.
/// </summary>
/// <param name="client">The BlobServiceClient.</param>
/// <returns>The BlobServiceClient's HttpPipeline.</returns>
protected static HttpPipeline GetHttpPipeline(BlobServiceClient client) =>
client.ClientConfiguration.Pipeline;
/// <summary>
/// Get a <see cref="BlobServiceClient"/>'s authentication
/// <see cref="HttpPipelinePolicy"/> for creating child clients.
/// </summary>
/// <param name="client">The BlobServiceClient.</param>
/// <returns>The BlobServiceClient's authentication policy.</returns>
protected static HttpPipelinePolicy GetAuthenticationPolicy(BlobServiceClient client) =>
client.AuthenticationPolicy;
/// <summary>
/// Get a <see cref="BlobServiceClient"/>'s <see cref="BlobClientOptions"/>
/// for creating child clients.
/// </summary>
/// <param name="client">The BlobServiceClient.</param>
/// <returns>The BlobServiceClient's BlobClientOptions.</returns>
protected static BlobClientOptions GetClientOptions(BlobServiceClient client) =>
new BlobClientOptions(client.ClientConfiguration.Version)
{
// We only use this for communicating diagnostics, at the moment
Diagnostics =
{
IsDistributedTracingEnabled = client.ClientConfiguration.ClientDiagnostics.IsActivityEnabled
}
};
#endregion protected static accessors for Azure.Storage.Blobs.Batch
#region GetBlobContainers
/// <summary>
/// The <see cref="GetBlobContainers(BlobContainerTraits, BlobContainerStates, string, CancellationToken)"/>
/// operation returns a sequence of blob containers in the storage account. Enumerating the
/// blob containers may make multiple requests to the service while fetching
/// all the values. Containers are ordered lexicographically by name.
///
/// For more information,
/// see <see href="https://docs.microsoft.com/rest/api/storageservices/list-containers2">
/// List Containers</see>.
/// </summary>
/// <param name="traits">
/// Specifies trait options for shaping the blob containers.
/// </param>
/// <param name="states">
/// Specifies state options for shaping the blob containers.
/// </param>
/// <param name="prefix">
/// Specifies a string that filters the results to return only containers
/// whose name begins with the specified <paramref name="prefix"/>.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> of <see cref="Response{BlobContainerItem}"/>
/// describing the blob containers in the storage account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual Pageable<BlobContainerItem> GetBlobContainers(
BlobContainerTraits traits = BlobContainerTraits.None,
BlobContainerStates states = BlobContainerStates.None,
string prefix = default,
CancellationToken cancellationToken = default) =>
new GetBlobContainersAsyncCollection(this, traits, states, prefix).ToSyncCollection(cancellationToken);
/// <summary>
/// The <see cref="GetBlobContainers(BlobContainerTraits, string, CancellationToken)"/> operation returns an async
/// sequence of blob containers in the storage account. Enumerating the
/// blob containers may make multiple requests to the service while fetching
/// all the values. Containers are ordered lexicographically by name.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/rest/api/storageservices/list-containers2">
/// List Containers</see>.
/// </summary>
/// <param name="traits">
/// Specifies trait options for shaping the blob containers.
/// </param>
/// <param name="prefix">
/// Specifies a string that filters the results to return only containers
/// whose name begins with the specified <paramref name="prefix"/>.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// An <see cref="IEnumerable{T}"/> of <see cref="Response{BlobContainerItem}"/>
/// describing the blob containers in the storage account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
#pragma warning disable AZC0002 // DO ensure all service methods, both asynchronous and synchronous, take an optional CancellationToken parameter called cancellationToken.
public virtual Pageable<BlobContainerItem> GetBlobContainers(
#pragma warning restore AZC0002 // DO ensure all service methods, both asynchronous and synchronous, take an optional CancellationToken parameter called cancellationToken.
BlobContainerTraits traits,
string prefix,
CancellationToken cancellationToken) =>
new GetBlobContainersAsyncCollection(this, traits, default, prefix).ToSyncCollection(cancellationToken);
/// <summary>
/// The <see cref="GetBlobContainersAsync(BlobContainerTraits, BlobContainerStates, string, CancellationToken)"/>
/// operation returns an async sequence of blob containers in the storage account. Enumerating the
/// blob containers may make multiple requests to the service while fetching
/// all the values. Containers are ordered lexicographically by name.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/rest/api/storageservices/list-containers2">
/// List Containers</see>.
/// </summary>
/// <param name="traits">
/// Specifies trait options for shaping the blob containers.
/// </param>
/// <param name="states">
/// Specifies states options for shaping the blob containers.
/// </param>
/// <param name="prefix">
/// Specifies a string that filters the results to return only containers
/// whose name begins with the specified <paramref name="prefix"/>.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// An <see cref="AsyncPageable{T}"/> describing the
/// containers in the storage account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual AsyncPageable<BlobContainerItem> GetBlobContainersAsync(
BlobContainerTraits traits = BlobContainerTraits.None,
BlobContainerStates states = BlobContainerStates.None,
string prefix = default,
CancellationToken cancellationToken = default) =>
new GetBlobContainersAsyncCollection(this, traits, states, prefix).ToAsyncCollection(cancellationToken);
/// <summary>
/// The <see cref="GetBlobContainersAsync(BlobContainerTraits, string, CancellationToken)"/>
/// operation returns an async sequence of blob containers in the storage account. Enumerating the
/// blob containers may make multiple requests to the service while fetching
/// all the values. Containers are ordered lexicographically by name.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/rest/api/storageservices/list-containers2">
/// List Containers</see>.
/// </summary>
/// <param name="traits">
/// Specifies trait options for shaping the blob containers.
/// </param>
/// <param name="prefix">
/// Specifies a string that filters the results to return only containers
/// whose name begins with the specified <paramref name="prefix"/>.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// An <see cref="AsyncPageable{T}"/> describing the
/// containers in the storage account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
#pragma warning disable AZC0002 // DO ensure all service methods, both asynchronous and synchronous, take an optional CancellationToken parameter called cancellationToken.
public virtual AsyncPageable<BlobContainerItem> GetBlobContainersAsync(
#pragma warning restore AZC0002 // DO ensure all service methods, both asynchronous and synchronous, take an optional CancellationToken parameter called cancellationToken.
BlobContainerTraits traits,
string prefix,
CancellationToken cancellationToken) =>
new GetBlobContainersAsyncCollection(this, traits, default, prefix).ToAsyncCollection(cancellationToken);
/// <summary>
/// The <see cref="GetBlobContainersInternal"/> operation returns a
/// single segment of blob containers in the storage account, starting
/// from the specified <paramref name="continuationToken"/>. Use an empty
/// <paramref name="continuationToken"/> to start enumeration from the beginning
/// and the <see cref="ListContainersSegmentResponse.NextMarker"/> if it's not
/// empty to make subsequent calls to <see cref="GetBlobContainersInternal"/>
/// to continue enumerating the containers segment by segment.
/// Containers are ordered lexicographically by name.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/rest/api/storageservices/list-containers2">
/// List Containers</see>.
/// </summary>
/// <param name="continuationToken">
/// An optional string value that identifies the segment of the list
/// of blob containers to be returned with the next listing operation. The
/// operation returns a non-empty <see cref="ListContainersSegmentResponse.NextMarker"/>
/// if the listing operation did not return all blob containers remaining
/// to be listed with the current segment. The NextMarker value can
/// be used as the value for the <paramref name="continuationToken"/> parameter
/// in a subsequent call to request the next segment of list items.
/// </param>
/// <param name="traits">
/// Specifies trait options for shaping the blob containers.
/// </param>
/// <param name="states">
/// Specifies state options for shaping the blob containers.
/// </param>
/// <param name="prefix">
/// Specifies a string that filters the results to return only containers
/// whose name begins with the specified <paramref name="prefix"/>.
/// </param>
/// <param name="pageSizeHint">
/// Gets or sets a value indicating the size of the page that should be
/// requested.
/// </param>
/// <param name="async">
/// Whether to invoke the operation asynchronously.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// A <see cref="Response{BlobContainersSegment}"/> describing a
/// segment of the blob containers in the storage account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
internal async Task<Response<ListContainersSegmentResponse>> GetBlobContainersInternal(
string continuationToken,
BlobContainerTraits traits,
#pragma warning disable CA1801 // Review unused parameters
BlobContainerStates states,
#pragma warning restore CA1801 // Review unused parameters
string prefix,
int? pageSizeHint,
bool async,
CancellationToken cancellationToken)
{
using (ClientConfiguration.Pipeline.BeginLoggingScope(nameof(BlobServiceClient)))
{
ClientConfiguration.Pipeline.LogMethodEnter(
nameof(BlobServiceClient),
message:
$"{nameof(Uri)}: {Uri}\n" +
$"{nameof(continuationToken)}: {continuationToken}\n" +
$"{nameof(traits)}: {traits}");
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(BlobServiceClient)}.{nameof(GetBlobContainers)}");
try
{
scope.Start();
ResponseWithHeaders<ListContainersSegmentResponse, ServiceListContainersSegmentHeaders> response;
if (async)
{
response = await ServiceRestClient.ListContainersSegmentAsync(
prefix: prefix,
marker: continuationToken,
maxresults: pageSizeHint,
include: BlobExtensions.AsIncludeItems(traits, states),
cancellationToken: cancellationToken)
.ConfigureAwait(false);
}
else
{
response = ServiceRestClient.ListContainersSegment(
prefix: prefix,
marker: continuationToken,
maxresults: pageSizeHint,
include: BlobExtensions.AsIncludeItems(traits, states),
cancellationToken: cancellationToken);
}
ListContainersSegmentResponse listContainersResponse = response.Value;
if ((traits & BlobContainerTraits.Metadata) != BlobContainerTraits.Metadata)
{
List<ContainerItemInternal> containerItemInternals = response.Value.ContainerItems.Select(r => new ContainerItemInternal(
r.Name,
r.Deleted,
r.Version,
r.Properties,
metadata: null))
.ToList();
listContainersResponse = new ListContainersSegmentResponse(
response.Value.ServiceEndpoint,
response.Value.Prefix,
response.Value.Marker,
response.Value.MaxResults,
containerItemInternals.AsReadOnly(),
response.Value.NextMarker);
}
return Response.FromValue(
listContainersResponse,
response.GetRawResponse());
}
catch (Exception ex)
{
ClientConfiguration.Pipeline.LogException(ex);
scope.Failed(ex);
throw;
}
finally
{
ClientConfiguration.Pipeline.LogMethodExit(nameof(BlobServiceClient));
scope.Dispose();
}
}
}
#endregion GetBlobContainers
#region GetAccountInfo
/// <summary>
/// The <see cref="GetAccountInfo"/> operation returns the sku
/// name and account kind for the specified account.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/get-account-information">
/// Get Account Information</see>.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// A <see cref="Response{AccountInfo}"/> describing the account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual Response<AccountInfo> GetAccountInfo(
CancellationToken cancellationToken = default) =>
GetAccountInfoInternal(
false, // async
cancellationToken)
.EnsureCompleted();
/// <summary>
/// The <see cref="GetAccountInfoAsync"/> operation returns the sku
/// name and account kind for the specified account.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/get-account-information">
/// Get Account Information</see>.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// A <see cref="Response{AccountInfo}"/> describing the account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
public virtual async Task<Response<AccountInfo>> GetAccountInfoAsync(
CancellationToken cancellationToken = default) =>
await GetAccountInfoInternal(
true, // async
cancellationToken)
.ConfigureAwait(false);
/// <summary>
/// The <see cref="GetAccountInfoInternal"/> operation returns the sku
/// name and account kind for the specified account.
///
/// For more information, see
/// <see href="https://docs.microsoft.com/en-us/rest/api/storageservices/get-account-information">
/// Get Account Information</see>.
/// </summary>
/// <param name="async">
/// Whether to invoke the operation asynchronously.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>
/// A <see cref="Response{AccountInfo}"/> describing the account.
/// </returns>
/// <remarks>
/// A <see cref="RequestFailedException"/> will be thrown if
/// a failure occurs.
/// </remarks>
private async Task<Response<AccountInfo>> GetAccountInfoInternal(
bool async,
CancellationToken cancellationToken)
{
using (ClientConfiguration.Pipeline.BeginLoggingScope(nameof(BlobServiceClient)))
{
ClientConfiguration.Pipeline.LogMethodEnter(nameof(BlobServiceClient), message: $"{nameof(Uri)}: {Uri}");
DiagnosticScope scope = ClientConfiguration.ClientDiagnostics.CreateScope($"{nameof(BlobServiceClient)}.{nameof(GetAccountInfo)}");
try
{
scope.Start();
ResponseWithHeaders<ServiceGetAccountInfoHeaders> response;
if (async)
{
response = await ServiceRestClient.GetAccountInfoAsync(