-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ShareClient.ts
1223 lines (1175 loc) · 40 KB
/
ShareClient.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
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.
import { HttpResponse, isNode } from "@azure/core-http";
import { CanonicalCode } from "@opentelemetry/api";
import { AbortSignalLike } from "@azure/abort-controller";
import {
DeleteSnapshotsOptionType,
DirectoryCreateResponse,
DirectoryDeleteResponse,
FileCreateResponse,
FileDeleteResponse,
ShareCreatePermissionResponse,
ShareCreateResponse,
ShareCreateSnapshotResponse,
ShareDeleteResponse,
ShareGetAccessPolicyHeaders,
ShareGetPermissionResponse,
ShareGetPropertiesResponse,
ShareSetAccessPolicyResponse,
ShareSetMetadataResponse,
ShareSetQuotaResponse,
SignedIdentifierModel,
ShareGetStatisticsResponseModel
} from "./generatedModels";
import { Share } from "./generated/src/operations";
import { Metadata } from "./models";
import { newPipeline, StoragePipelineOptions, Pipeline } from "./Pipeline";
import { StorageClient, CommonOptions } from "./StorageClient";
import { URLConstants } from "./utils/constants";
import {
appendToURLPath,
setURLParameter,
truncatedISO8061Date,
extractConnectionStringParts,
getShareNameAndPathFromUrl
} from "./utils/utils.common";
import {
ShareDirectoryClient,
DirectoryCreateOptions,
DirectoryDeleteOptions
} from "./ShareDirectoryClient";
import { FileCreateOptions, FileDeleteOptions, ShareFileClient } from "./ShareFileClient";
import { Credential } from "./credentials/Credential";
import { StorageSharedKeyCredential } from "./credentials/StorageSharedKeyCredential";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
import { createSpan } from "./utils/tracing";
/**
* Options to configure the {@link ShareClient.create} operation.
*
* @export
* @interface ShareCreateOptions
*/
export interface ShareCreateOptions 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 ShareCreateOptions
*/
abortSignal?: AbortSignalLike;
/**
* A name-value pair to associate with a file storage object.
*
* @type {{ [propertyName: string]: string }}
* @memberof ShareCreateOptions
*/
metadata?: { [propertyName: string]: string };
/**
* Specifies the maximum size of the share, in
* gigabytes.
*
* @type {number}
* @memberof ShareCreateOptions
*/
quota?: number;
}
/**
* Options to configure the {@link ShareClient.delete} operation.
*
* @export
* @interface ShareDeleteMethodOptions
*/
export interface ShareDeleteMethodOptions 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 ShareDeleteMethodOptions
*/
abortSignal?: AbortSignalLike;
/**
* Specifies the option
* include to delete the base share and all of its snapshots. Possible values
* include: 'include'
*
* @type {DeleteSnapshotsOptionType}
* @memberof ShareDeleteMethodOptions
*/
deleteSnapshots?: DeleteSnapshotsOptionType;
}
/**
* Options to configure the {@link ShareClient.setMetadata} operation.
*
* @export
* @interface ShareSetMetadataOptions
*/
export interface ShareSetMetadataOptions 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 ShareSetMetadataOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link ShareClient.setAccessPolicy} operation.
*
* @export
* @interface ShareSetAccessPolicyOptions
*/
export interface ShareSetAccessPolicyOptions 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 ShareSetAccessPolicyOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link ShareClient.getAccessPolicy} operation.
*
* @export
* @interface ShareGetAccessPolicyOptions
*/
export interface ShareGetAccessPolicyOptions 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 ShareGetAccessPolicyOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link ShareClient.exists} operation.
*
* @export
* @interface ShareExistsOptions
*/
export interface ShareExistsOptions 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 ShareExistsOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link ShareClient.getProperties} operation.
*
* @export
* @interface ShareGetPropertiesOptions
*/
export interface ShareGetPropertiesOptions 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 ShareGetPropertiesOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link ShareClient.setQuota} operation.
*
* @export
* @interface ShareSetQuotaOptions
*/
export interface ShareSetQuotaOptions 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 ShareSetQuotaOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link ShareClient.getStatistics} operation.
*
* @export
* @interface ShareGetStatisticsOptions
*/
export interface ShareGetStatisticsOptions 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 ShareGetStatisticsOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Signed Identifier
*
* @export
* @interface SignedIdentifier
*/
export interface SignedIdentifier {
/**
* @member {string} id a unique id
*/
id: string;
/**
* @member {AccessPolicy} accessPolicy
*/
accessPolicy: {
/**
* @member {Date} startsOn the date-time the policy is active.
*/
startsOn: Date;
/**
* @member {string} expiresOn the date-time the policy expires.
*/
expiresOn: Date;
/**
* @member {string} permissions the permissions for the acl policy
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-share-acl
*/
permissions: string;
};
}
export declare type ShareGetAccessPolicyResponse = {
signedIdentifiers: SignedIdentifier[];
} & ShareGetAccessPolicyHeaders & {
/**
* The underlying HTTP response.
*/
_response: HttpResponse & {
/**
* The parsed HTTP response headers.
*/
parsedHeaders: ShareGetAccessPolicyHeaders;
/**
* The response body as text (string format)
*/
bodyAsText: string;
/**
* The response body as parsed JSON or XML
*/
parsedBody: SignedIdentifierModel[];
};
};
/**
* Options to configure the {@link ShareClient.createSnapshot} operation.
*
* @export
* @interface ShareCreateSnapshotOptions
*/
export interface ShareCreateSnapshotOptions 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 ShareCreateSnapshotOptions
*/
abortSignal?: AbortSignalLike;
/**
* A name-value pair to associate with a file storage object.
*
* @type {{ [propertyName: string]: string }}
* @memberof ShareCreateOptions
*/
metadata?: { [propertyName: string]: string };
}
/**
* Options to configure the {@link ShareClient.createPermission} operation.
*
* @export
* @interface ShareCreatePermissionOptions
*/
export interface ShareCreatePermissionOptions 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 ShareCreatePermissionOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Options to configure the {@link ShareClient.getPermission} operation.
*
* @export
* @interface ShareGetPermissionOptions
*/
export interface ShareGetPermissionOptions 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 ShareGetPermissionOptions
*/
abortSignal?: AbortSignalLike;
}
/**
* Response data for the {@link ShareClient.getStatistics} Operation.
*
* @export
* @interface ShareGetStatisticsResponse
*/
export type ShareGetStatisticsResponse = ShareGetStatisticsResponseModel & {
/**
* @deprecated shareUsage is going to be deprecated. Please use ShareUsageBytes instead.
*
* The approximate size of the data stored on the share, rounded up to the nearest gigabyte. Note
* that this value may not include all recently created or recently resized files.
*
* @type {number}
* @memberof ShareGetStatisticsResponse
*/
shareUsage: number;
};
/**
* Contains response data for the {@link ShareClient.createIfNotExists} operation.
*
* @export
* @interface ShareCreateIfNotExistsResponse
*/
export interface ShareCreateIfNotExistsResponse extends ShareCreateResponse {
/**
* Indicate whether the share is successfully created. Is false when the share is not changed as it already exists.
*
* @type {boolean}
* @memberof ShareCreateIfNotExistsResponse
*/
succeeded: boolean;
}
/**
* Contains response data for the {@link ShareClient.deleteIfExists} operation.
*
* @export
* @interface ShareDeleteIfExistsResponse
*/
export interface ShareDeleteIfExistsResponse extends ShareDeleteResponse {
/**
* Indicate whether the share is successfully deleted. Is false if the share does not exist in the first place.
*
* @type {boolean}
* @memberof ShareDeleteIfExistsResponse
*/
succeeded: boolean;
}
/**
* A ShareClient represents a URL to the Azure Storage share allowing you to manipulate its directories and files.
*
* @export
* @class ShareClient
*/
export class ShareClient extends StorageClient {
/**
* Share operation context provided by protocol layer.
*
* @private
* @type {Share}
* @memberof ShareClient
*/
private context: Share;
private _name: string;
/**
* The name of the share
*
* @type {string}
* @memberof ShareClient
*/
public get name(): string {
return this._name;
}
/**
* @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 {string} name Share name.
* @param {StoragePipelineOptions} [options] Optional. Options to configure the HTTP pipeline.
* @memberof ShareClient
*/
constructor(connectionString: string, name: string, options?: StoragePipelineOptions);
/**
* Creates an instance of ShareClient.
*
* @param {string} url A URL string pointing to Azure Storage file share, such as
* "https://myaccount.file.core.windows.net/share". You can
* append a SAS if using AnonymousCredential, such as
* "https://myaccount.file.core.windows.net/share?sasString".
* @param {Credential} [credential] Such as AnonymousCredential or StorageSharedKeyCredential.
* If not specified, AnonymousCredential is used.
* @param {StoragePipelineOptions} [options] Optional. Options to configure the HTTP pipeline.
* @memberof ShareClient
*/
constructor(url: string, credential?: Credential, options?: StoragePipelineOptions);
/**
* Creates an instance of ShareClient.
*
* @param {string} url A URL string pointing to Azure Storage file share, such as
* "https://myaccount.file.core.windows.net/share". You can
* append a SAS if using AnonymousCredential, such as
* "https://myaccount.file.core.windows.net/share?sasString".
* @param {Pipeline} pipeline Call newPipeline() to create a default
* pipeline, or provide a customized pipeline.
* @memberof ShareClient
*/
constructor(url: string, pipeline: Pipeline);
constructor(
urlOrConnectionString: string,
credentialOrPipelineOrShareName?: Credential | Pipeline | string,
options?: StoragePipelineOptions
) {
let pipeline: Pipeline;
let url: string;
if (credentialOrPipelineOrShareName instanceof Pipeline) {
// (url: string, pipeline: Pipeline)
url = urlOrConnectionString;
pipeline = credentialOrPipelineOrShareName;
} else if (credentialOrPipelineOrShareName instanceof Credential) {
// (url: string, credential?: Credential, options?: StoragePipelineOptions)
url = urlOrConnectionString;
pipeline = newPipeline(credentialOrPipelineOrShareName, options);
} else if (
!credentialOrPipelineOrShareName &&
typeof credentialOrPipelineOrShareName !== "string"
) {
// (url: string, credential?: Credential, options?: StoragePipelineOptions)
// The second parameter is undefined. Use anonymous credential.
url = urlOrConnectionString;
pipeline = newPipeline(new AnonymousCredential(), options);
} else if (
credentialOrPipelineOrShareName &&
typeof credentialOrPipelineOrShareName === "string"
) {
// (connectionString: string, name: string, options?: StoragePipelineOptions)
const extractedCreds = extractConnectionStringParts(urlOrConnectionString);
const name = credentialOrPipelineOrShareName;
if (extractedCreds.kind === "AccountConnString") {
if (isNode) {
const sharedKeyCredential = new StorageSharedKeyCredential(
extractedCreds.accountName!,
extractedCreds.accountKey
);
url = appendToURLPath(extractedCreds.url, name);
pipeline = newPipeline(sharedKeyCredential, options);
} else {
throw new Error("Account connection string is only supported in Node.js environment");
}
} else if (extractedCreds.kind === "SASConnString") {
url = appendToURLPath(extractedCreds.url, name) + "?" + extractedCreds.accountSas;
pipeline = newPipeline(new AnonymousCredential(), options);
} else {
throw new Error(
"Connection string must be either an Account connection string or a SAS connection string"
);
}
} else {
throw new Error("Expecting non-empty strings for name parameter");
}
super(url, pipeline);
this._name = getShareNameAndPathFromUrl(this.url).shareName;
this.context = new Share(this.storageClientContext);
}
/**
* Creates a new ShareClient object identical to the source but with the specified snapshot timestamp.
* Provide "" will remove the snapshot and return a URL to the base share.
*
* @param {string} snapshot The snapshot timestamp.
* @returns {ShareClient} A new ShareClient object identical to the source but with the specified snapshot timestamp
* @memberof ShareClient
*/
public withSnapshot(snapshot: string): ShareClient {
return new ShareClient(
setURLParameter(
this.url,
URLConstants.Parameters.SHARE_SNAPSHOT,
snapshot.length === 0 ? undefined : snapshot
),
this.pipeline
);
}
/**
* Creates a new share under the specified account. If the share with
* the same name already exists, the operation fails.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-share
*
* @param {ShareCreateOptions} [options] Options to Share Create operation.
* @returns {Promise<ShareCreateResponse>} Response data for the Share Create operation.
* @memberof ShareClient
*/
public async create(options: ShareCreateOptions = {}): Promise<ShareCreateResponse> {
const { span, spanOptions } = createSpan("ShareClient-create", options.tracingOptions);
try {
return await this.context.create({
...options,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new share under the specified account. If the share with
* the same name already exists, it is not changed.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-share
*
* @param {ShareCreateOptions} [options]
* @returns {Promise<ShareCreateIfNotExistsResponse>}
* @memberof ShareClient
*/
public async createIfNotExists(
options: ShareCreateOptions = {}
): Promise<ShareCreateIfNotExistsResponse> {
const { span, spanOptions } = createSpan(
"ShareClient-createIfNotExists",
options.tracingOptions
);
try {
const res = await this.create({
...options,
tracingOptions: { ...options!.tracingOptions, spanOptions }
});
return {
succeeded: true,
...res
};
} catch (e) {
if (e.details?.errorCode === "ShareAlreadyExists") {
span.setStatus({
code: CanonicalCode.ALREADY_EXISTS,
message: "Expected exception when creating a share only if it doesn't already exist."
});
return {
succeeded: false,
...e.response?.parsedHeaders,
_response: e.response
};
}
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a {@link ShareDirectoryClient} object.
*
* @param directoryName A directory name
* @returns {ShareDirectoryClient} The ShareDirectoryClient object for the given directory name.
* @memberof ShareClient
*/
public getDirectoryClient(directoryName: string): ShareDirectoryClient {
return new ShareDirectoryClient(
appendToURLPath(this.url, encodeURIComponent(directoryName)),
this.pipeline
);
}
/**
* Gets the directory client for the root directory of this share.
* Note that the root directory always exists and cannot be deleted.
*
* @readonly
* @type {ShareDirectoryClient} A new ShareDirectoryClient object for the root directory.
* @memberof ShareClient
*/
public get rootDirectoryClient(): ShareDirectoryClient {
return this.getDirectoryClient("");
}
/**
* Creates a new subdirectory under this share.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-directory
*
* @param {string} directoryName
* @param {DirectoryCreateOptions} [options] Options to Directory Create operation.
* @returns {Promise<{ directoryClient: ShareDirectoryClient, directoryCreateResponse: DirectoryCreateResponse }>} Directory creation response data and the corresponding directory client.
* @memberof ShareClient
*/
public async createDirectory(
directoryName: string,
options: DirectoryCreateOptions = {}
): Promise<{
directoryClient: ShareDirectoryClient;
directoryCreateResponse: DirectoryCreateResponse;
}> {
const { span, spanOptions } = createSpan("ShareClient-createDirectory", options.tracingOptions);
try {
const directoryClient = this.getDirectoryClient(directoryName);
const directoryCreateResponse = await directoryClient.create({
...options,
tracingOptions: { ...options.tracingOptions, spanOptions }
});
return {
directoryClient,
directoryCreateResponse
};
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Removes the specified empty sub directory under this share.
* Note that the directory must be empty before it can be deleted.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/delete-directory
*
* @param {string} directoryName
* @param {DirectoryDeleteOptions} [options] Options to Directory Delete operation.
* @returns {Promise<DirectoryDeleteResponse>} Directory deletion response data.
* @memberof ShareClient
*/
public async deleteDirectory(
directoryName: string,
options: DirectoryDeleteOptions = {}
): Promise<DirectoryDeleteResponse> {
const { span, spanOptions } = createSpan("ShareClient-deleteDirectory", options.tracingOptions);
try {
const directoryClient = this.getDirectoryClient(directoryName);
return await directoryClient.delete({
...options,
tracingOptions: { ...options.tracingOptions, spanOptions }
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a new file or replaces a file under the root directory of this share.
* Note it only initializes the file with no content.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-file
*
* @param {string} fileName
* @param {number} size Specifies the maximum size in bytes for the file, up to 1 TB.
* @param {FileCreateOptions} [options] Options to File Create operation.
* @returns {Promise<{ fileClient: ShareFileClient, fileCreateResponse: FileCreateResponse }>} File creation response data and the corresponding file client.
* @memberof ShareClient
*/
public async createFile(
fileName: string,
size: number,
options: FileCreateOptions = {}
): Promise<{ fileClient: ShareFileClient; fileCreateResponse: FileCreateResponse }> {
const { span, spanOptions } = createSpan("ShareClient-createFile", options.tracingOptions);
try {
const directoryClient = this.rootDirectoryClient;
const fileClient = directoryClient.getFileClient(fileName);
const fileCreateResponse = await fileClient.create(size, {
...options,
tracingOptions: { ...options.tracingOptions, spanOptions }
});
return {
fileClient,
fileCreateResponse
};
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Removes a file under the root directory of this share from the storage account.
* When a file is successfully deleted, it is immediately removed from the storage
* account's index and is no longer accessible to clients. The file's data is later
* removed from the service during garbage collection.
*
* Delete File will fail with status code 409 (Conflict) and error code `SharingViolation`
* if the file is open on an SMB client.
*
* Delete File is not supported on a share snapshot, which is a read-only copy of
* a share. An attempt to perform this operation on a share snapshot will fail with 400
* (`InvalidQueryParameterValue`)
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/delete-file2
*
* @param {string} directoryName
* @param {string} fileName
* @param {FileDeleteOptions} [options] Options to File Delete operation.
* @returns Promise<FileDeleteResponse> File Delete response data.
* @memberof ShareClient
*/
public async deleteFile(
fileName: string,
options: FileDeleteOptions = {}
): Promise<FileDeleteResponse> {
const { span, spanOptions } = createSpan("ShareClient-deleteFile", options.tracingOptions);
try {
const directoryClient = this.rootDirectoryClient;
const fileClient = directoryClient.getFileClient(fileName);
return await fileClient.delete({
...options,
tracingOptions: { ...options.tracingOptions, spanOptions }
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Returns true if the Azrue share resource represented by this client exists; false otherwise.
*
* NOTE: use this function with care since an existing share might be deleted by other clients or
* applications. Vice versa new shares might be added by other clients or applications after this
* function completes.
*
* @param {ShareExistsOptions} [options] options to Exists operation.
* @returns {Promise<boolean>}
* @memberof ShareClient
*/
public async exists(options: ShareExistsOptions = {}): Promise<boolean> {
const { span, spanOptions } = createSpan("ShareClient-exists", options.tracingOptions);
try {
await this.getProperties({
abortSignal: options.abortSignal,
tracingOptions: { ...options.tracingOptions, spanOptions }
});
return true;
} catch (e) {
if (e.statusCode === 404) {
span.setStatus({
code: CanonicalCode.NOT_FOUND,
message: "Expected exception when checking share existence"
});
return false;
}
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Returns all user-defined metadata and system properties for the specified
* share.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-share-properties
*
* WARNING: The `metadata` object returned in the response will have its keys in lowercase, even if
* they originally contained uppercase characters. This differs from the metadata keys returned by
* the `listShares` method of {@link ShareServiceClient} using the `includeMetadata` option, which
* will retain their original casing.
*
* @returns {Promise<ShareGetPropertiesResponse>} Response data for the Share Get Properties operation.
* @memberof ShareClient
*/
public async getProperties(
options: ShareGetPropertiesOptions = {}
): Promise<ShareGetPropertiesResponse> {
const { span, spanOptions } = createSpan("ShareClient-getProperties", options.tracingOptions);
try {
return await this.context.getProperties({
abortSignal: options.abortSignal,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Marks the specified share for deletion. The share and any directories or files
* contained within it are later deleted during garbage collection.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/delete-share
*
* @param {ShareDeleteMethodOptions} [options] Options to Share Delete operation.
* @returns {Promise<ShareDeleteResponse>} Response data for the Share Delete operation.
* @memberof ShareClient
*/
public async delete(options: ShareDeleteMethodOptions = {}): Promise<ShareDeleteResponse> {
const { span, spanOptions } = createSpan("ShareClient-delete", options.tracingOptions);
try {
return await this.context.deleteMethod({
...options,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Marks the specified share for deletion if it exists. The share and any directories or files
* contained within it are later deleted during garbage collection.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/delete-share
*
* @param {ShareDeleteMethodOptions} [options]
* @returns {Promise<ShareDeleteIfExistsResponse>}
* @memberof ShareClient
*/
public async deleteIfExists(
options: ShareDeleteMethodOptions = {}
): Promise<ShareDeleteIfExistsResponse> {
const { span, spanOptions } = createSpan("ShareClient-deleteIfExists", options.tracingOptions);
try {
const res = await this.delete({
...options,
tracingOptions: { ...options!.tracingOptions, spanOptions }
});
return {
succeeded: true,
...res
};
} catch (e) {
if (e.details?.errorCode === "ShareNotFound") {
span.setStatus({
code: CanonicalCode.NOT_FOUND,
message: "Expected exception when deleting a share only if it exists."
});
return {
succeeded: false,
...e.response?.parsedHeaders,
_response: e.response
};
}
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Sets one or more user-defined name-value pairs for the specified share.
*
* If no option provided, or no metadata defined in the option parameter, the share
* metadata will be removed.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-share-metadata
*
* @param {Metadata} [metadata] If no metadata provided, all existing directory metadata will be removed.
* @param {ShareSetMetadataOptions} [option] Options to Share Set Metadata operation.
* @returns {Promise<ShareSetMetadataResponse>} Response data for the Share Set Metadata operation.
* @memberof ShareClient
*/
public async setMetadata(
metadata?: Metadata,
options: ShareSetMetadataOptions = {}
): Promise<ShareSetMetadataResponse> {
const { span, spanOptions } = createSpan("ShareClient-setMetadata", options.tracingOptions);
try {
return await this.context.setMetadata({
abortSignal: options.abortSignal,
metadata,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Gets the permissions for the specified share. The permissions indicate
* whether share data may be accessed publicly.
*
* WARNING: JavaScript Date will potential lost precision when parsing start and expiry string.
* For example, new Date("2018-12-31T03:44:23.8827891Z").toISOString() will get "2018-12-31T03:44:23.882Z".
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-share-acl
*
* @param {ShareGetAccessPolicyOptions} [option] Options to Share Get Access Policy operation.
* @returns {Promise<ShareGetAccessPolicyResponse>} Response data for the Share Get Access Policy operation.
* @memberof ShareClient
*/
public async getAccessPolicy(
options: ShareGetAccessPolicyOptions = {}
): Promise<ShareGetAccessPolicyResponse> {
const { span, spanOptions } = createSpan("ShareClient-getAccessPolicy", options.tracingOptions);
try {
const response = await this.context.getAccessPolicy({
abortSignal: options.abortSignal,
spanOptions
});
const res: ShareGetAccessPolicyResponse = {
_response: response._response,
date: response.date,
etag: response.etag,
lastModified: response.lastModified,
requestId: response.requestId,
signedIdentifiers: [],
version: response.version
};
for (const identifier of response) {
res.signedIdentifiers.push({
accessPolicy: {
expiresOn: new Date(identifier.accessPolicy!.expiresOn!),
permissions: identifier.accessPolicy!.permissions!,
startsOn: new Date(identifier.accessPolicy!.startsOn!)
},
id: identifier.id
});
}