-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
clients.ts
1750 lines (1655 loc) · 58.8 KB
/
clients.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 { HttpRequestBody, isNode, TokenCredential } from "@azure/core-http";
import { BlobClient } from "@azure/storage-blob";
import { CanonicalCode } from "@opentelemetry/api";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
import { StorageSharedKeyCredential } from "./credentials/StorageSharedKeyCredential";
import { DataLakeLeaseClient } from "./DataLakeLeaseClient";
import { PathOperations } from "./generated/src/operations";
import {
DirectoryCreateOptions,
DirectoryCreateIfNotExistsOptions,
DirectoryCreateResponse,
FileAppendOptions,
FileAppendResponse,
FileCreateIfNotExistsOptions,
FileCreateOptions,
FileCreateResponse,
FileFlushOptions,
FileFlushResponse,
FileParallelUploadOptions,
FileReadOptions,
FileReadResponse,
FileReadToBufferOptions,
FileUploadResponse,
Metadata,
PathAccessControlItem,
PathCreateOptions,
PathCreateIfNotExistsOptions,
PathCreateResponse,
PathDeleteOptions,
PathDeleteResponse,
PathExistsOptions,
PathGetAccessControlOptions,
PathGetAccessControlResponse,
PathGetPropertiesAction,
PathGetPropertiesOptions,
PathGetPropertiesResponse,
PathHttpHeaders,
PathMoveOptions,
PathMoveResponse,
PathPermissions,
PathRenameMode,
PathResourceType,
PathSetAccessControlOptions,
PathSetAccessControlResponse,
PathSetHttpHeadersOptions,
PathSetHttpHeadersResponse,
PathSetMetadataOptions,
PathSetMetadataResponse,
PathSetPermissionsOptions,
PathSetPermissionsResponse,
PathCreateIfNotExistsResponse,
PathDeleteIfExistsResponse,
DirectoryCreateIfNotExistsResponse,
FileCreateIfNotExistsResponse
} from "./models";
import { newPipeline, Pipeline, StoragePipelineOptions } from "./Pipeline";
import { StorageClient } from "./StorageClient";
import {
toAclString,
toPathGetAccessControlResponse,
toPermissionsString,
toProperties
} from "./transforms";
import { createSpan } from "./utils/tracing";
import { appendToURLPath, setURLPath } from "./utils/utils.common";
import { Readable } from "stream";
import {
DEFAULT_HIGH_LEVEL_CONCURRENCY,
FILE_MAX_SINGLE_UPLOAD_THRESHOLD,
FILE_UPLOAD_MAX_CHUNK_SIZE,
FILE_MAX_SIZE_BYTES,
FILE_UPLOAD_DEFAULT_CHUNK_SIZE,
BLOCK_BLOB_MAX_BLOCKS,
ETagAny
} from "./utils/constants";
import { BufferScheduler } from "./utils/BufferScheduler";
import { Batch } from "./utils/Batch";
import { fsStat, fsCreateReadStream } from "./utils/utils.node";
/**
* A DataLakePathClient represents a URL to the Azure Storage path (directory or file).
*
* @export
* @class DataLakePathClient
* @extends {StorageClient}
*/
export class DataLakePathClient extends StorageClient {
/**
* pathContext provided by protocol layer.
*
* @private
* @type {PathOperations}
* @memberof DataLakePathClient
*/
private pathContext: PathOperations;
/**
* blobClient provided by @azure/storage-blob package.
*
* @private
* @type {BlobClient}
* @memberof DataLakePathClient
*/
private blobClient: BlobClient;
/**
* Creates an instance of DataLakePathClient from url and credential.
*
* @param {string} url A Client string pointing to Azure Storage data lake path (directory or file), such as
* "https://myaccount.dfs.core.windows.net/filesystem/directory" or "https://myaccount.dfs.core.windows.net/filesystem/file".
* You can append a SAS if using AnonymousCredential, such as "https://myaccount.dfs.core.windows.net/filesystem/directory?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 DataLakePathClient
*/
public constructor(
url: string,
credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential,
options?: StoragePipelineOptions
);
/**
* Creates an instance of DataLakePathClient from url and pipeline.
*
* @param {string} url A Client string pointing to Azure Storage data lake path (directory or file), such as
* "https://myaccount.dfs.core.windows.net/filesystem/directory" or "https://myaccount.dfs.core.windows.net/filesystem/file".
* You can append a SAS if using AnonymousCredential, such as "https://myaccount.dfs.core.windows.net/filesystem/directory?sasString".
* @param {Pipeline} pipeline Call newPipeline() to create a default
* pipeline, or provide a customized pipeline.
* @memberof DataLakePathClient
*/
public constructor(url: string, pipeline: Pipeline);
public constructor(
url: string,
credentialOrPipeline?:
| StorageSharedKeyCredential
| AnonymousCredential
| TokenCredential
| Pipeline,
options?: StoragePipelineOptions
) {
if (credentialOrPipeline instanceof Pipeline) {
super(url, credentialOrPipeline);
} else {
let credential;
if (credentialOrPipeline === undefined) {
credential = new AnonymousCredential();
} else {
credential = credentialOrPipeline;
}
const pipeline = newPipeline(credential, options);
super(url, pipeline);
}
this.pathContext = new PathOperations(this.storageClientContext);
this.blobClient = new BlobClient(this.blobEndpointUrl, this.pipeline);
}
/**
* Name of current file system.
*
* @readonly
* @type {string}
* @memberof DataLakePathClient
*/
public get fileSystemName(): string {
return this.blobClient.containerName;
}
/**
* Name of current path (directory or file).
*
* @readonly
* @type {string}
* @memberof DataLakePathClient
*/
public get name(): string {
return this.blobClient.name;
}
/**
* Convert current DataLakePathClient to DataLakeDirectoryClient if current path is a directory.
*
* @returns {DataLakeDirectoryClient}
* @memberof DataLakePathClient
*/
public toDirectoryClient(): DataLakeDirectoryClient {
return new DataLakeDirectoryClient(this.dfsEndpointUrl, this.pipeline);
}
/**
* Convert current DataLakePathClient to DataLakeFileClient if current path is a file.
*
* @returns {DataLakeFileClient}
* @memberof DataLakePathClient
*/
public toFileClient(): DataLakeFileClient {
return new DataLakeFileClient(this.dfsEndpointUrl, this.pipeline);
}
/**
* Get a {@link DataLakeLeaseClient} that manages leases on the path (directory or file).
*
* @param {string} [proposeLeaseId] Optional. Initial proposed lease Id.
* @returns {DataLakeLeaseClient}
* @memberof DataLakePathClient
*/
public getDataLakeLeaseClient(proposeLeaseId?: string): DataLakeLeaseClient {
return new DataLakeLeaseClient(this.blobClient.getBlobLeaseClient(proposeLeaseId));
}
/**
* Create a directory or path.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {PathResourceType} resourceType Resource type, "directory" or "file".
* @param {PathCreateOptions} [options={}] Optional. Options when creating path.
* @returns {Promise<PathCreateResponse>}
* @memberof DataLakePathClient
*/
public async create(
resourceType: PathResourceType,
options: PathCreateOptions = {}
): Promise<PathCreateResponse> {
options.conditions = options.conditions || {};
const { span, spanOptions } = createSpan("DataLakePathClient-create", options.tracingOptions);
try {
return await this.pathContext.create({
...options,
resource: resourceType,
leaseAccessConditions: options.conditions,
modifiedAccessConditions: options.conditions,
properties: toProperties(options.metadata),
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Create a directory or file. If the resource already exists, it is not changed.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {PathResourceType} resourceType Resource type, "directory" or "file".
* @param {PathCreateOptions} [options={}]
* @returns {Promise<PathCreateIfNotExistsResponse>}
* @memberof DataLakePathClient
*/
public async createIfNotExists(
resourceType: PathResourceType,
options: PathCreateIfNotExistsOptions = {}
): Promise<PathCreateIfNotExistsResponse> {
const { span, spanOptions } = createSpan(
"DataLakePathClient-createIfNotExists",
options.tracingOptions
);
try {
const conditions = { ifNoneMatch: ETagAny };
const res = await this.create(resourceType, {
...options,
conditions,
tracingOptions: { ...options!.tracingOptions, spanOptions }
});
return {
succeeded: true,
...res
};
} catch (e) {
if (e.details?.errorCode === "PathAlreadyExists") {
span.setStatus({
code: CanonicalCode.ALREADY_EXISTS,
message: "Expected exception when creating a blob only if it does not already exist."
});
return {
succeeded: false,
...e.response?.parsedHeaders,
_response: e.response
};
}
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Returns true if the Data Lake file represented by this client exists; false otherwise.
*
* NOTE: use this function with care since an existing file might be deleted by other clients or
* applications. Vice versa new files might be added by other clients or applications after this
* function completes.
*
* @param {PathExistsOptions} [options] options to Exists operation.
* @returns {Promise<boolean>}
* @memberof DataLakePathClient
*/
public async exists(options: PathExistsOptions = {}): Promise<boolean> {
const { span, spanOptions } = createSpan("DataLakeFileClient-exists", options.tracingOptions);
try {
return await this.blobClient.exists({
...options,
tracingOptions: { ...options!.tracingOptions, spanOptions }
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Delete current path (directory or file).
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/delete
*
* @param {boolean} [recursive] Required and valid only when the resource is a directory. If "true", all paths beneath the directory will be deleted.
* @param {PathDeleteOptions} [options={}] Optional. Options when deleting path.
* @returns {Promise<PathDeleteResponse>}
* @memberof DataLakePathClient
*/
public async delete(
recursive?: boolean,
options: PathDeleteOptions = {}
): Promise<PathDeleteResponse> {
options.conditions = options.conditions || {};
const { span, spanOptions } = createSpan("DataLakePathClient-delete", options.tracingOptions);
try {
let continuation;
let response;
// How to handle long delete loop?
do {
response = await this.pathContext.deleteMethod({
continuation,
recursive,
leaseAccessConditions: options.conditions,
modifiedAccessConditions: options.conditions,
spanOptions
});
continuation = response.continuation;
} while (continuation !== undefined && continuation !== "");
return response;
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Delete current path (directory or file) if it exists.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/delete
*
* @param {boolean} [recursive] Required and valid only when the resource is a directory. If "true", all paths beneath the directory will be deleted.
* @param {PathDeleteOptions} [options={}]
* @returns {Promise<PathDeleteIfExistsResponse>}
* @memberof DataLakePathClient
*/
public async deleteIfExists(
recursive?: boolean,
options: PathDeleteOptions = {}
): Promise<PathDeleteIfExistsResponse> {
options.conditions = options.conditions || {};
const { span, spanOptions } = createSpan(
"DataLakePathClient-deleteIfExists",
options.tracingOptions
);
try {
const res = await this.delete(recursive, {
...options,
tracingOptions: { ...options!.tracingOptions, spanOptions }
});
return {
succeeded: true,
...res
};
} catch (e) {
if (e.details?.errorCode === "PathNotFound") {
span.setStatus({
code: CanonicalCode.NOT_FOUND,
message: "Expected exception when deleting a directory or file 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();
}
}
/**
* Returns the access control data for a path (directory of file).
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/getproperties
*
* @param {PathGetAccessControlOptions} [options={}] Optional. Options when getting file access control.
* @returns {Promise<PathGetAccessControlResponse>}
* @memberof DataLakePathClient
*/
public async getAccessControl(
options: PathGetAccessControlOptions = {}
): Promise<PathGetAccessControlResponse> {
options.conditions = options.conditions || {};
const { span, spanOptions } = createSpan(
"DataLakePathClient-getAccessControl",
options.tracingOptions
);
try {
const response = await this.pathContext.getProperties({
action: PathGetPropertiesAction.GetAccessControl,
upn: options.userPrincipalName,
leaseAccessConditions: options.conditions,
modifiedAccessConditions: options.conditions,
spanOptions
});
return toPathGetAccessControlResponse(response);
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Set the access control data for a path (directory of file).
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/update
*
* @param {PathAccessControlItem[]} acl The POSIX access control list for the file or directory.
* @param {PathSetAccessControlOptions} [options={}] Optional. Options when setting path access control.
* @returns {Promise<PathSetAccessControlResponse>}
* @memberof DataLakePathClient
*/
public async setAccessControl(
acl: PathAccessControlItem[],
options: PathSetAccessControlOptions = {}
): Promise<PathSetAccessControlResponse> {
options.conditions = options.conditions || {};
const { span, spanOptions } = createSpan(
"DataLakePathClient-setAccessControl",
options.tracingOptions
);
try {
return await this.pathContext.setAccessControl({
...options,
acl: toAclString(acl),
leaseAccessConditions: options.conditions,
modifiedAccessConditions: options.conditions,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Sets the file permissions on a path.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/update
*
* @param {PathPermissions} permissions The POSIX access permissions for the file owner, the file owning group, and others.
* @param {PathSetPermissionsOptions} [options={}] Optional. Options when setting path permissions.
* @returns {Promise<PathSetPermissionsResponse>}
* @memberof DataLakePathClient
*/
public async setPermissions(
permissions: PathPermissions,
options: PathSetPermissionsOptions = {}
): Promise<PathSetPermissionsResponse> {
options.conditions = options.conditions || {};
const { span, spanOptions } = createSpan(
"DataLakePathClient-setPermissions",
options.tracingOptions
);
try {
return await this.pathContext.setAccessControl({
...options,
permissions: toPermissionsString(permissions),
leaseAccessConditions: options.conditions,
modifiedAccessConditions: options.conditions,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Returns all user-defined metadata, standard HTTP properties, and system properties
* for the path (directory or file).
*
* 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 methods of {@link DataLakeFileSystemClient} that list paths using the `includeMetadata` option, which
* will retain their original casing.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-properties
*
* @param {PathGetPropertiesOptions} [options={}] Optional. Options when getting path properties.
* @returns {Promise<PathGetPropertiesResponse>}
* @memberof DataLakePathClient
*/
public async getProperties(
options: PathGetPropertiesOptions = {}
): Promise<PathGetPropertiesResponse> {
const { span, spanOptions } = createSpan(
"DataLakePathClient-getProperties",
options.tracingOptions
);
try {
return await this.blobClient.getProperties({
...options,
customerProvidedKey: undefined, // Doesn't support customer provided key in data lake package yet
tracingOptions: { ...options.tracingOptions, spanOptions }
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Sets system properties on the path (directory or file).
*
* If no value provided, or no value provided for the specified blob HTTP headers,
* these blob HTTP headers without a value will be cleared.
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-blob-properties
*
* @param {PathHttpHeaders} httpHeaders
* @param {PathSetHttpHeadersOptions} [options={}]
* @returns {Promise<PathSetHttpHeadersResponse>}
* @memberof DataLakePathClient
*/
public async setHttpHeaders(
httpHeaders: PathHttpHeaders,
options: PathSetHttpHeadersOptions = {}
): Promise<PathSetHttpHeadersResponse> {
const { span, spanOptions } = createSpan(
"DataLakePathClient-setHttpHeaders",
options.tracingOptions
);
try {
return await this.blobClient.setHTTPHeaders(
{
blobCacheControl: httpHeaders.cacheControl,
blobContentType: httpHeaders.contentType,
blobContentMD5: httpHeaders.contentMD5,
blobContentEncoding: httpHeaders.contentEncoding,
blobContentLanguage: httpHeaders.contentLanguage,
blobContentDisposition: httpHeaders.contentDisposition
},
{
...options,
tracingOptions: { ...options.tracingOptions, spanOptions }
}
);
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Sets user-defined metadata for the specified path (directory of file) as one or more name-value pairs.
*
* If no option provided, or no metadata defined in the parameter, the path
* metadata will be removed.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/set-blob-metadata
*
* @param {Metadata} [metadata] Optional. Replace existing metadata with this value.
* If no value provided the existing metadata will be removed.
* @param {PathSetMetadataOptions} [options={}] Optional. Options when setting path metadata.
* @returns {Promise<PathSetMetadataResponse>}
* @memberof DataLakePathClient
*/
public async setMetadata(
metadata?: Metadata,
options: PathSetMetadataOptions = {}
): Promise<PathSetMetadataResponse> {
const { span, spanOptions } = createSpan(
"DataLakePathClient-setMetadata",
options.tracingOptions
);
try {
return await this.blobClient.setMetadata(metadata, {
...options,
customerProvidedKey: undefined, // Doesn't support customer provided key in data lake package yet
tracingOptions: { ...options.tracingOptions, spanOptions }
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Move directory or file within same file system.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {string} destinationPath Destination directory path like "directory" or file path "directory/file"
* @param {PathMoveOptions} [options] Optional. Options when moving directory or file.
* @returns {Promise<PathMoveResponse>}
* @memberof DataLakePathClient
*/
public async move(destinationPath: string, options?: PathMoveOptions): Promise<PathMoveResponse>;
/**
* Move directory or file to another file system.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {string} destinationFileSystem Destination file system like "filesystem".
* @param {string} destinationPath Destination directory path like "directory" or file path "directory/file"
* @param {PathMoveOptions} [options] Optional. Options when moving directory or file.
* @returns {Promise<PathMoveResponse>}
* @memberof DataLakePathClient
*/
public async move(
destinationFileSystem: string,
destinationPath: string,
options?: PathMoveOptions
): Promise<PathMoveResponse>;
public async move(
destinationPathOrFileSystem: string,
destinationPathOrOptions?: string | PathMoveOptions,
options?: PathMoveOptions
): Promise<PathMoveResponse> {
let destinationFileSystem = this.fileSystemName;
let destinationPath = destinationPathOrFileSystem;
if (typeof destinationPathOrOptions === "string") {
destinationFileSystem = destinationPathOrFileSystem;
destinationPath = destinationPathOrOptions;
options = options || {};
} else {
options = destinationPathOrOptions || {};
}
options.conditions = options.conditions || {};
options.destinationConditions = options.destinationConditions || {};
const { span, spanOptions } = createSpan("DataLakePathClient-move", options.tracingOptions);
const renameSource = `/${this.fileSystemName}/${this.name}`; // TODO: Confirm number of /
const renameDestination = `/${destinationFileSystem}/${destinationPath}`; // TODO: Confirm encoding
const destinationUrl = setURLPath(this.dfsEndpointUrl, renameDestination);
const destPathClient = new DataLakePathClient(destinationUrl, this.pipeline);
try {
return await destPathClient.pathContext.create({
mode: PathRenameMode.Legacy, // By default
renameSource,
sourceLeaseId: options.conditions.leaseId,
leaseAccessConditions: options.destinationConditions,
sourceModifiedAccessConditions: {
sourceIfMatch: options.conditions.ifMatch,
sourceIfNoneMatch: options.conditions.ifNoneMatch,
sourceIfModifiedSince: options.conditions.ifModifiedSince,
sourceIfUnmodifiedSince: options.conditions.ifUnmodifiedSince
},
modifiedAccessConditions: options.destinationConditions,
spanOptions
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
}
/**
* A DataLakeDirectoryClient represents a URL to the Azure Storage directory.
*
* @export
* @class DataLakeDirectoryClient
* @extends {DataLakePathClient}
*/
export class DataLakeDirectoryClient extends DataLakePathClient {
// https://stackoverflow.com/questions/50729485/override-method-with-different-argument-types-in-extended-class-typescript
/**
* Create a directory.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {PathResourceType} resourceType Resource type, must be "directory" for DataLakeDirectoryClient.
* @param {PathCreateOptions} [options] Optional. Options when creating directory.
* @returns {Promise<PathCreateResponse>}
* @memberof DataLakeDirectoryClient
*/
public async create(
resourceType: PathResourceType,
options?: PathCreateOptions
): Promise<PathCreateResponse>;
/**
* Create a directory.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {DirectoryCreateOptions} [options] Optional. Options when creating directory.
* @returns {Promise<DirectoryCreateResponse>}
* @memberof DataLakeDirectoryClient
*/
public async create(options?: DirectoryCreateOptions): Promise<DirectoryCreateResponse>;
public async create(
resourceTypeOrOptions?: PathResourceType | PathCreateOptions,
options: PathCreateOptions = {}
): Promise<PathCreateResponse> {
if (resourceTypeOrOptions === PathResourceType.Directory) {
return super.create(resourceTypeOrOptions as PathResourceType, options);
}
if (resourceTypeOrOptions === PathResourceType.File) {
throw TypeError(
`DataLakeDirectoryClient:create() resourceType cannot be ${PathResourceType.File}. Refer to DataLakeFileClient for file creation.`
);
}
options = resourceTypeOrOptions || {};
options.conditions = options.conditions || {};
const { span, spanOptions } = createSpan(
"DataLakeDirectoryClient-create",
options.tracingOptions
);
try {
return await super.create(PathResourceType.Directory, {
...options,
tracingOptions: {
...options.tracingOptions,
spanOptions
}
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Create a directory if it doesn't already exists.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {PathResourceType} resourceType Resource type, must be "directory" for DataLakeDirectoryClient.
* @param {PathCreateIfNotExistsOptions} [options]
* @returns {Promise<PathCreateIfNotExistsResponse>}
* @memberof DataLakeDirectoryClient
*/
public async createIfNotExists(
resourceType: PathResourceType,
options?: PathCreateIfNotExistsOptions
): Promise<PathCreateIfNotExistsResponse>;
/**
* Create a directory if it doesn't already exists.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*
* @param {DirectoryCreateIfNotExistsOptions} [options]
* @returns {Promise<DirectoryCreateIfNotExistsResponse>}
* @memberof DataLakeDirectoryClient
*/
public async createIfNotExists(
options?: DirectoryCreateIfNotExistsOptions
): Promise<DirectoryCreateIfNotExistsResponse>;
public async createIfNotExists(
resourceTypeOrOptions?: PathResourceType | PathCreateIfNotExistsOptions,
options: PathCreateIfNotExistsOptions = {}
): Promise<PathCreateIfNotExistsResponse> {
if (resourceTypeOrOptions === PathResourceType.File) {
throw TypeError(
`DataLakeDirectoryClient:createIfNotExists() resourceType cannot be ${resourceTypeOrOptions}. Refer to DataLakeFileClient for file creation.`
);
}
if (resourceTypeOrOptions !== PathResourceType.Directory) {
options = resourceTypeOrOptions || {};
}
const { span, spanOptions } = createSpan(
"DataLakeDirectoryClient-createIfNotExists",
options.tracingOptions
);
try {
return await super.createIfNotExists(PathResourceType.Directory, {
...options,
tracingOptions: {
...options.tracingOptions,
spanOptions
}
});
} catch (e) {
span.setStatus({
code: CanonicalCode.UNKNOWN,
message: e.message
});
throw e;
} finally {
span.end();
}
}
/**
* Creates a {@link DataLakeDirectoryClient} object under current directory.
*
* @param {string} subdirectoryName Subdirectory name.
* @returns {DataLakeDirectoryClient}
* @memberof DataLakeDirectoryClient
*/
public getSubdirectoryClient(subdirectoryName: string): DataLakeDirectoryClient {
return new DataLakeDirectoryClient(
appendToURLPath(this.url, encodeURIComponent(subdirectoryName)),
this.pipeline
);
}
/**
* Creates a {@link DataLakeFileClient} object under current directory.
*
* @param {string} fileName
* @returns {DataLakeFileClient}
* @memberof DataLakeDirectoryClient
*/
public getFileClient(fileName: string): DataLakeFileClient {
return new DataLakeFileClient(
appendToURLPath(this.url, encodeURIComponent(fileName)),
this.pipeline
);
}
}
/**
* A DataLakeFileClient represents a URL to the Azure Storage file.
*
* @export
* @class DataLakeFileClient
* @extends {DataLakePathClient}
*/
export class DataLakeFileClient extends DataLakePathClient {
/**
* pathContextInternal provided by protocol layer.
*
* @private
* @type {PathOperations}
* @memberof DataLakeFileClient
*/
private pathContextInternal: PathOperations;
/**
* blobClientInternal provided by @azure/storage-blob package.
*
* @private
* @type {BlobClient}
* @memberof DataLakeFileClient
*/
private blobClientInternal: BlobClient;
/**
* Creates an instance of DataLakeFileClient from url and credential.
*
* @param {string} url A Client string pointing to Azure Storage data lake file, such as
* "https://myaccount.dfs.core.windows.net/filesystem/file".
* You can append a SAS if using AnonymousCredential, such as "https://myaccount.dfs.core.windows.net/filesystem/directory/file?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 DataLakeFileClient
*/
public constructor(
url: string,
credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential,
options?: StoragePipelineOptions
);
/**
* Creates an instance of DataLakeFileClient from url and pipeline.
*
* @param {string} url A Client string pointing to Azure Storage data lake file, such as
* "https://myaccount.dfs.core.windows.net/filesystem/file".
* You can append a SAS if using AnonymousCredential, such as "https://myaccount.dfs.core.windows.net/filesystem/directory/file?sasString".
* @param {Pipeline} pipeline Call newPipeline() to create a default
* pipeline, or provide a customized pipeline.
* @memberof DataLakeFileClient
*/
public constructor(url: string, pipeline: Pipeline);
public constructor(
url: string,
credentialOrPipeline?:
| StorageSharedKeyCredential
| AnonymousCredential
| TokenCredential
| Pipeline,
options?: StoragePipelineOptions
) {
if (credentialOrPipeline instanceof Pipeline) {
super(url, credentialOrPipeline);
} else {
let credential;
if (credentialOrPipeline === undefined) {
credential = new AnonymousCredential();
} else {
credential = credentialOrPipeline;
}
const pipeline = newPipeline(credential, options);
super(url, pipeline);
}
this.pathContextInternal = new PathOperations(this.storageClientContext);
this.blobClientInternal = new BlobClient(this.blobEndpointUrl, this.pipeline);
}
/**
* Create a file.
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/datalakestoragegen2/path/create
*