-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BlobServiceClient.ts
914 lines (882 loc) · 31 KB
/
BlobServiceClient.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
TokenCredential,
isTokenCredential,
isNode,
HttpResponse,
getDefaultProxySettings
} from "@azure/core-http";
import { CanonicalCode } from "@opentelemetry/types";
import { AbortSignalLike } from "@azure/abort-controller";
import {
ServiceGetUserDelegationKeyHeaders,
ContainerCreateResponse,
ContainerDeleteResponse,
ServiceGetPropertiesResponse,
BlobServiceProperties,
ServiceSetPropertiesResponse,
ServiceGetStatisticsResponse,
ServiceGetAccountInfoResponse,
ServiceListContainersSegmentResponse,
ContainerItem,
ListContainersIncludeType,
UserDelegationKeyModel
} from "./generatedModels";
import { Service } from "./generated/src/operations";
import { newPipeline, StoragePipelineOptions, Pipeline } from "./Pipeline";
import { ContainerClient, ContainerCreateOptions, ContainerDeleteMethodOptions } from "./Clients";
import { appendToURLPath, extractConnectionStringParts } from "./utils/utils.common";
import { StorageSharedKeyCredential } from "./credentials/StorageSharedKeyCredential";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
import "@azure/core-paging";
import { PageSettings, PagedAsyncIterableIterator } from "@azure/core-paging";
import { truncatedISO8061Date } from "./utils/utils.common";
import { createSpan } from "./utils/tracing";
import { BlobBatchClient } from "./BlobBatchClient";
import { CommonOptions, StorageClient } from "./StorageClient";
import { getCachedDefaultHttpClient } from "./utils/cache";
/**
* Options to configure the {@link BlobServiceClient.getProperties} operation.
*
* @export
* @interface ServiceGetPropertiesOptions
*/
export interface ServiceGetPropertiesOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof ServiceGetPropertiesOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link BlobServiceClient.setProperties} operation.
*
* @export
* @interface ServiceSetPropertiesOptions
*/
export interface ServiceSetPropertiesOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof ServiceSetPropertiesOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link BlobServiceClient.getAccountInfo} operation.
*
* @export
* @interface ServiceGetAccountInfoOptions
*/
export interface ServiceGetAccountInfoOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof ServiceGetAccountInfoOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link BlobServiceClient.getStatistics} operation.
*
* @export
* @interface ServiceGetStatisticsOptions
*/
export interface ServiceGetStatisticsOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof ServiceGetStatisticsOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the Service - Get User Delegation Key.
*
* @export
* @interface ServiceGetUserDelegationKeyOptions
*/
export interface ServiceGetUserDelegationKeyOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof ServiceGetStatisticsOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link BlobServiceClient.listContainerSegment} operation.
*
* @interface ServiceListContainersSegmentOptions
*/
interface ServiceListContainersSegmentOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof ServiceListContainersSegmentOptions
*/
abortSignal?: AbortSignalLike;
/**
* Filters the results to return only containers
* whose name begins with the specified prefix.
*/
prefix?: string;
/**
* Specifies the maximum number of containers
* to return. If the request does not specify maxPageSize, or specifies a
* value greater than 5000, the server will return up to 5000 items. Note
* that if the listing operation crosses a partition boundary, then the
* service will return a continuation token for retrieving the remainder of
* the results. For this reason, it is possible that the service will return
* fewer results than specified by maxPageSize, or than the default of 5000.
*/
maxPageSize?: number;
/**
* Include this parameter to
* specify that the container's metadata be returned as part of the response
* body. Possible values include: 'metadata'
*/
include?: ListContainersIncludeType;
}
/**
* Options to configure the {@link BlobServiceClient.listContainers} operation.
*
* @export
* @interface ServiceListContainersOptions
*/
export interface ServiceListContainersOptions extends CommonOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof ServiceListContainersOptions
*/
abortSignal?: AbortSignalLike;
/**
* Filters the results to return only containers
* whose name begins with the specified prefix.
*/
prefix?: string;
/**
* Specifies whether the container's metadata
* should be returned as part of the response body.
*/
includeMetadata?: boolean;
}
/**
* A user delegation key.
*/
export interface UserDelegationKey {
/**
* The Azure Active Directory object ID in GUID format.
*
* @type {string}
* @memberof UserDelegationKey
*/
signedObjectId: string;
/**
* The Azure Active Directory tenant ID in GUID format.
*
* @type {string}
* @memberof UserDelegationKey
*/
signedTenantId: string;
/**
* The date-time the key is active.
*
* @type {Date}
* @memberof UserDelegationKey
*/
signedStartsOn: Date;
/**
* The date-time the key expires.
*
* @type {Date}
* @memberof UserDelegationKey
*/
signedExpiresOn: Date;
/**
* Abbreviation of the Azure Storage service that accepts the key.
*
* @type {string}
* @memberof UserDelegationKey
*/
signedService: string;
/**
* The service version that created the key.
*
* @type {string}
* @memberof UserDelegationKey
*/
signedVersion: string;
/**
* The key as a base64 string.
*
* @type {string}
* @memberof UserDelegationKey
*/
value: string;
}
/**
* Contains response data for the {@link getUserDelegationKey} operation.
*/
export declare type ServiceGetUserDelegationKeyResponse = UserDelegationKey &
ServiceGetUserDelegationKeyHeaders & {
/**
* The underlying HTTP response.
*/
_response: HttpResponse & {
/**
* The parsed HTTP response headers.
*/
parsedHeaders: ServiceGetUserDelegationKeyHeaders;
/**
* The response body as text (string format)
*/
bodyAsText: string;
/**
* The response body as parsed JSON or XML
*/
parsedBody: UserDelegationKeyModel;
};
};
/**
* A BlobServiceClient represents a Client to the Azure Storage Blob service allowing you
* to manipulate blob containers.
*
* @export
* @class BlobServiceClient
*/
export class BlobServiceClient extends StorageClient {
/**
* serviceContext provided by protocol layer.
*
* @private
* @type {Service}
* @memberof BlobServiceClient
*/
private serviceContext: Service;
/**
*
* Creates an instance of BlobServiceClient from connection string.
*
* @param {string} connectionString Account connection string or a SAS connection string of an Azure storage account.
* [ Note - Account connection string can only be used in NODE.JS runtime. ]
* Account connection string example -
* `DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=accountKey;EndpointSuffix=core.windows.net`
* SAS connection string example -
* `BlobEndpoint=https://myaccount.blob.core.windows.net/;QueueEndpoint=https://myaccount.queue.core.windows.net/;FileEndpoint=https://myaccount.file.core.windows.net/;TableEndpoint=https://myaccount.table.core.windows.net/;SharedAccessSignature=sasString`
* @param {StoragePipelineOptions} [options] Optional. Options to configure the HTTP pipeline.
* @memberof BlobServiceClient
*/
public static fromConnectionString(connectionString: string, options?: StoragePipelineOptions) {
options = options || {};
const extractedCreds = extractConnectionStringParts(connectionString);
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey
);
options.proxyOptions = getDefaultProxySettings(extractedCreds.proxyUri);
const pipeline = newPipeline(sharedKeyCredential, options);
return new BlobServiceClient(extractedCreds.url, pipeline);
} else {
throw new Error("Account connection string is only supported in Node.js environment");
}
} else if (extractedCreds.kind === "SASConnString") {
const pipeline = newPipeline(new AnonymousCredential(), options);
return new BlobServiceClient(extractedCreds.url + "?" + extractedCreds.accountSas, pipeline);
} else {
throw new Error(
"Connection string must be either an Account connection string or a SAS connection string"
);
}
}
/**
* Creates an instance of BlobServiceClient.
*
* @param {string} url A Client string pointing to Azure Storage blob service, such as
* "https://myaccount.blob.core.windows.net". You can append a SAS
* if using AnonymousCredential, such as "https://myaccount.blob.core.windows.net?sasString".
* @param {StorageSharedKeyCredential | AnonymousCredential | TokenCredential} credential Such as AnonymousCredential, StorageSharedKeyCredential or any credential from the @azure/identity package to authenticate requests to the service. You can also provide an object that implements the TokenCredential interface. If not specified, AnonymousCredential is used.
* @param {StoragePipelineOptions} [options] Optional. Options to configure the HTTP pipeline.
* @memberof BlobServiceClient
*
* Example using DefaultAzureCredential from `@azure/identity`:
*
* ```js
* const account = "<storage account name>";
*
* const defaultAzureCredential = new DefaultAzureCredential();
*
* const blobServiceClient = new BlobServiceClient(
* `https://${account}.blob.core.windows.net`,
* defaultAzureCredential
* );
* ```
*
* Example using an account name/key:
*
* ```js
* const account = "<storage account name>"
* const sharedKeyCredential = new StorageSharedKeyCredential(account, "<account key>");
*
* const blobServiceClient = new BlobServiceClient(
* `https://${account}.blob.core.windows.net`,
* sharedKeyCredential
* );
* ```
*/
constructor(
url: string,
credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential,
options?: StoragePipelineOptions
);
/**
* Creates an instance of BlobServiceClient.
*
* @param {string} url A Client string pointing to Azure Storage blob service, such as
* "https://myaccount.blob.core.windows.net". You can append a SAS
* if using AnonymousCredential, such as "https://myaccount.blob.core.windows.net?sasString".
* @param {Pipeline} pipeline Call newPipeline() to create a default
* pipeline, or provide a customized pipeline.
* @memberof BlobServiceClient
*/
constructor(url: string, pipeline: Pipeline);
constructor(
url: string,
credentialOrPipeline?:
| StorageSharedKeyCredential
| AnonymousCredential
| TokenCredential
| Pipeline,
options?: StoragePipelineOptions
) {
// when options.httpClient is not specified, passing in a DefaultHttpClient instance to
// avoid each client creating its own http client.
const newOptions: StoragePipelineOptions = {
httpClient: getCachedDefaultHttpClient(),
...options
};
let pipeline: Pipeline;
if (credentialOrPipeline instanceof Pipeline) {
pipeline = credentialOrPipeline;
} else if (
(isNode && credentialOrPipeline instanceof StorageSharedKeyCredential) ||
credentialOrPipeline instanceof AnonymousCredential ||
isTokenCredential(credentialOrPipeline)
) {
pipeline = newPipeline(credentialOrPipeline, newOptions);
} else {
// The second parameter is undefined. Use anonymous credential
pipeline = newPipeline(new AnonymousCredential(), newOptions);
}
super(url, pipeline);
this.serviceContext = new Service(this.storageClientContext);
}
/**
* Creates a {@link ContainerClient} object
*
* @param {string} containerName A container name
* @returns {ContainerClient} A new ContainerClient object for the given container name.
* @memberof BlobServiceClient
*
* Example usage:
*
* ```js
* const containerClient = blobServiceClient.getContainerClient("<container name>");
* ```
*/
public getContainerClient(containerName: string): ContainerClient {
return new ContainerClient(
appendToURLPath(this.url, encodeURIComponent(containerName)),
this.pipeline
);
}
/**
* Create a Blob container.
*
* @param {string} containerName Name of the container to create.
* @param {ContainerCreateOptions} [options] Options to configure Container Create operation.
* @returns {Promise<{ containerClient: ContainerClient; containerCreateResponse: ContainerCreateResponse }>} Container creation response and the corresponding container client.
* @memberof BlobServiceClient
*/
public async createContainer(
containerName: string,
options: ContainerCreateOptions = {}
): Promise<{
containerClient: ContainerClient;
containerCreateResponse: ContainerCreateResponse;
}> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-createContainer",
options.tracingOptions
);
try {
const containerClient = this.getContainerClient(containerName);
const containerCreateResponse = await containerClient.create({
...options,
tracingOptions: { ...options!.tracingOptions, spanOptions }
});
return {
containerClient,
containerCreateResponse
};
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Deletes a Blob container.
*
* @param {string} containerName Name of the container to delete.
* @param {ContainerDeleteMethodOptions} [options] Options to configure Container Delete operation.
* @returns {Promise<ContainerDeleteResponse>} Container deletion response.
* @memberof BlobServiceClient
*/
public async deleteContainer(
containerName: string,
options: ContainerDeleteMethodOptions = {}
): Promise<ContainerDeleteResponse> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-deleteContainer",
options.tracingOptions
);
try {
const containerClient = this.getContainerClient(containerName);
return await containerClient.delete({
...options,
tracingOptions: { ...options!.tracingOptions, spanOptions }
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Gets the properties of a storage account’s Blob service, including properties
* for Storage Analytics and CORS (Cross-Origin Resource Sharing) rules.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-service-properties
*
* @param {ServiceGetPropertiesOptions} [options] Options to the Service Get Properties operation.
* @returns {Promise<ServiceGetPropertiesResponse>} Response data for the Service Get Properties operation.
* @memberof BlobServiceClient
*/
public async getProperties(
options: ServiceGetPropertiesOptions = {}
): Promise<ServiceGetPropertiesResponse> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-getProperties",
options.tracingOptions
);
try {
return await this.serviceContext.getProperties({
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Sets properties for a storage account’s Blob service endpoint, including properties
* for Storage Analytics, CORS (Cross-Origin Resource Sharing) rules and soft delete settings.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-blob-service-properties}
*
* @param {BlobServiceProperties} properties
* @param {ServiceSetPropertiesOptions} [options] Options to the Service Set Properties operation.
* @returns {Promise<ServiceSetPropertiesResponse>} Response data for the Service Set Properties operation.
* @memberof BlobServiceClient
*/
public async setProperties(
properties: BlobServiceProperties,
options: ServiceSetPropertiesOptions = {}
): Promise<ServiceSetPropertiesResponse> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-setProperties",
options.tracingOptions
);
try {
return await this.serviceContext.setProperties(properties, {
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Retrieves statistics related to replication for the Blob service. It is only
* available on the secondary location endpoint when read-access geo-redundant
* replication is enabled for the storage account.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-service-stats}
*
* @param {ServiceGetStatisticsOptions} [options] Options to the Service Get Statistics operation.
* @returns {Promise<ServiceGetStatisticsResponse>} Response data for the Service Get Statistics operation.
* @memberof BlobServiceClient
*/
public async getStatistics(
options: ServiceGetStatisticsOptions = {}
): Promise<ServiceGetStatisticsResponse> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-getStatistics",
options.tracingOptions
);
try {
return await this.serviceContext.getStatistics({
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* The Get Account Information operation returns the sku name and account kind
* for the specified account.
* The Get Account Information operation is available on service versions beginning
* with version 2018-03-28.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-account-information
*
* @param {ServiceGetAccountInfoOptions} [options] Options to the Service Get Account Info operation.
* @returns {Promise<ServiceGetAccountInfoResponse>} Response data for the Service Get Account Info operation.
* @memberof BlobServiceClient
*/
public async getAccountInfo(
options: ServiceGetAccountInfoOptions = {}
): Promise<ServiceGetAccountInfoResponse> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-getAccountInfo",
options.tracingOptions
);
try {
return await this.serviceContext.getAccountInfo({
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Returns a list of the containers under the specified account.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/list-containers2
*
* @param {string} [marker] A string value that identifies the portion of
* the list of containers to be returned with the next listing operation. The
* operation returns the NextMarker value within the response body if the
* listing operation did not return all containers remaining to be listed
* with the current page. The NextMarker value can be used as the value for
* the marker parameter in a subsequent call to request the next page of list
* items. The marker value is opaque to the client.
* @param {ServiceListContainersSegmentOptions} [options] Options to the Service List Container Segment operation.
* @returns {Promise<ServiceListContainersSegmentResponse>} Response data for the Service List Container Segment operation.
* @memberof BlobServiceClient
*/
private async listContainersSegment(
marker?: string,
options: ServiceListContainersSegmentOptions = {}
): Promise<ServiceListContainersSegmentResponse> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-listContainersSegment",
options.tracingOptions
);
try {
return await this.serviceContext.listContainersSegment({
abortSignal: options.abortSignal,
marker,
...options,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Returns an AsyncIterableIterator for ServiceListContainersSegmentResponses
*
* @private
* @param {string} [marker] A string value that identifies the portion of
* the list of containers to be returned with the next listing operation. The
* operation returns the NextMarker value within the response body if the
* listing operation did not return all containers remaining to be listed
* with the current page. The NextMarker value can be used as the value for
* the marker parameter in a subsequent call to request the next page of list
* items. The marker value is opaque to the client.
* @param {ServiceListContainersSegmentOptions} [options] Options to list containers operation.
* @returns {AsyncIterableIterator<ServiceListContainersSegmentResponse>}
* @memberof BlobServiceClient
*/
private async *listSegments(
marker?: string,
options: ServiceListContainersSegmentOptions = {}
): AsyncIterableIterator<ServiceListContainersSegmentResponse> {
let listContainersSegmentResponse;
if (!!marker || marker === undefined) {
do {
listContainersSegmentResponse = await this.listContainersSegment(marker, options);
listContainersSegmentResponse.containerItems =
listContainersSegmentResponse.containerItems || [];
marker = listContainersSegmentResponse.continuationToken;
yield await listContainersSegmentResponse;
} while (marker);
}
}
/**
* Returns an AsyncIterableIterator for Container Items
*
* @private
* @param {ServiceListContainersSegmentOptions} [options] Options to list containers operation.
* @returns {AsyncIterableIterator<ContainerItem>}
* @memberof BlobServiceClient
*/
private async *listItems(
options: ServiceListContainersSegmentOptions = {}
): AsyncIterableIterator<ContainerItem> {
let marker: string | undefined;
for await (const segment of this.listSegments(marker, options)) {
yield* segment.containerItems;
}
}
/**
* Returns an async iterable iterator to list all the containers
* under the specified account.
*
* .byPage() returns an async iterable iterator to list the containers in pages.
*
* Example using `for await` syntax:
*
* ```js
* let i = 1;
* for await (const container of blobServiceClient.listContainers()) {
* console.log(`Container ${i++}: ${container.name}`);
* }
* ```
*
* Example using `iter.next()`:
*
* ```js
* let i = 1;
* const iter = blobServiceClient.listContainers();
* let containerItem = await iter.next();
* while (!containerItem.done) {
* console.log(`Container ${i++}: ${containerItem.value.name}`);
* containerItem = await iter.next();
* }
* ```
*
* Example using `byPage()`:
*
* ```js
* // passing optional maxPageSize in the page settings
* let i = 1;
* for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
* if (response.containerItems) {
* for (const container of response.containerItems) {
* console.log(`Container ${i++}: ${container.name}`);
* }
* }
* }
* ```
*
* Example using paging with a marker:
*
* ```js
* let i = 1;
* let iterator = blobServiceClient.listContainers().byPage({ maxPageSize: 2 });
* let response = (await iterator.next()).value;
*
* // Prints 2 container names
* if (response.containerItems) {
* for (const container of response.containerItems) {
* console.log(`Container ${i++}: ${container.name}`);
* }
* }
*
* // Gets next marker
* let marker = response.continuationToken;
* // Passing next marker as continuationToken
* iterator = blobServiceClient
* .listContainers()
* .byPage({ continuationToken: marker, maxPageSize: 10 });
* response = (await iterator.next()).value;
*
* // Prints 10 container names
* if (response.containerItems) {
* for (const container of response.containerItems) {
* console.log(`Container ${i++}: ${container.name}`);
* }
* }
* ```
*
* @param {ServiceListContainersOptions} [options={}] Options to list containers.
* @returns {PagedAsyncIterableIterator<ContainerItem, ServiceListContainersSegmentResponse>} An asyncIterableIterator that supports paging.
* @memberof BlobServiceClient
*/
public listContainers(
options: ServiceListContainersOptions = {}
): PagedAsyncIterableIterator<ContainerItem, ServiceListContainersSegmentResponse> {
if (options.prefix === "") {
options.prefix = undefined;
}
// AsyncIterableIterator to iterate over containers
const listSegmentOptions: ServiceListContainersSegmentOptions = {
...options,
...(options.includeMetadata ? { include: "metadata" } : {})
};
const iter = this.listItems(listSegmentOptions);
return {
/**
* @member {Promise} [next] The next method, part of the iteration protocol
*/
next() {
return iter.next();
},
/**
* @member {Symbol} [asyncIterator] The connection to the async iterator, part of the iteration protocol
*/
[Symbol.asyncIterator]() {
return this;
},
/**
* @member {Function} [byPage] Return an AsyncIterableIterator that works a page at a time
*/
byPage: (settings: PageSettings = {}) => {
return this.listSegments(settings.continuationToken, {
maxPageSize: settings.maxPageSize,
...listSegmentOptions
});
}
};
}
/**
* ONLY AVAILABLE WHEN USING BEARER TOKEN AUTHENTICATION (TokenCredential).
*
* Retrieves a user delegation key for the Blob service. This is only a valid operation when using
* bearer token authentication.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-user-delegation-key
*
* @param {Date} startsOn The start time for the user delegation SAS. Must be within 7 days of the current time
* @param {Date} expiresOn The end time for the user delegation SAS. Must be within 7 days of the current time
* @returns {Promise<ServiceGetUserDelegationKeyResponse>}
* @memberof BlobServiceClient
*/
public async getUserDelegationKey(
startsOn: Date,
expiresOn: Date,
options: ServiceGetUserDelegationKeyOptions = {}
): Promise<ServiceGetUserDelegationKeyResponse> {
const { span, spanOptions } = createSpan(
"BlobServiceClient-getUserDelegationKey",
options.tracingOptions
);
try {
const response = await this.serviceContext.getUserDelegationKey(
{
startsOn: truncatedISO8061Date(startsOn, false),
expiresOn: truncatedISO8061Date(expiresOn, false)
},
{
abortSignal: options.abortSignal,
spanOptions
}
);
const userDelegationKey = {
signedObjectId: response.signedObjectId,
signedTenantId: response.signedTenantId,
signedStartsOn: new Date(response.signedStartsOn),
signedExpiresOn: new Date(response.signedExpiresOn),
signedService: response.signedService,
signedVersion: response.signedVersion,
value: response.value
};
const res: ServiceGetUserDelegationKeyResponse = {
_response: response._response,
requestId: response.requestId,
clientRequestId: response.clientRequestId,
version: response.version,
date: response.date,
errorCode: response.errorCode,
...userDelegationKey
};
return res;
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a BlobBatchClient object to conduct batch operations.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/blob-batch
*
* @returns {BlobBatchClient} A new BlobBatchClient object for this service.
* @memberof BlobServiceClient
*/
public getBlobBatchClient(): BlobBatchClient {
return new BlobBatchClient(this.url, this.pipeline);
}
}