/
StorageClient.ts
116 lines (101 loc) · 4.11 KB
/
StorageClient.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { isNode, isTokenCredential, TokenCredential } from "@azure/core-http";
import { OperationTracingOptions } from "@azure/core-tracing";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
import { StorageSharedKeyCredential } from "./credentials/StorageSharedKeyCredential";
import { StorageClientContext } from "./generated/src/storageClientContext";
import { Pipeline } from "./Pipeline";
import { toBlobEndpointUrl, toDfsEndpointUrl } from "./transforms";
import { escapeURLPath, getAccountNameFromUrl, getURLScheme, iEqual } from "./utils/utils.common";
/**
* An interface for options common to every remote operation.
*/
export interface CommonOptions {
/**
* Options to configure spans created when tracing is enabled.
*/
tracingOptions?: OperationTracingOptions;
}
/**
* A StorageClient represents a based URL class for {@link BlobServiceClient}, {@link ContainerClient}
* and etc.
*/
export abstract class StorageClient {
/**
* Encoded URL string value.
*/
public readonly url: string;
public readonly accountName: string;
/**
* Encoded URL string value for corresponding blob endpoint.
*/
protected readonly blobEndpointUrl: string;
/**
* Encoded URL string value for corresponding dfs endpoint.
*/
protected readonly dfsEndpointUrl: string;
/**
* Request policy pipeline.
*
* @internal
*/
protected readonly pipeline: Pipeline;
/**
* 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.
*/
public readonly credential: StorageSharedKeyCredential | AnonymousCredential | TokenCredential;
/**
* StorageClient is a reference to protocol layer operations entry, which is
* generated by AutoRest generator.
*/
protected readonly storageClientContext: StorageClientContext;
/**
* storageClientContextWithBlobEndpoint is a reference to protocol layer operations entry, which is
* generated by AutoRest generator, with its url pointing to the Blob endpoint.
*/
protected readonly storageClientContextToBlobEndpoint: StorageClientContext;
/**
*/
protected readonly isHttps: boolean;
/**
* Creates an instance of StorageClient.
* @param url - url to resource
* @param pipeline - request policy pipeline.
*/
protected constructor(url: string, pipeline: Pipeline) {
// URL should be encoded and only once, protocol layer shouldn't encode URL again
this.url = escapeURLPath(url);
this.blobEndpointUrl = toBlobEndpointUrl(this.url);
this.dfsEndpointUrl = toDfsEndpointUrl(this.url);
this.accountName = getAccountNameFromUrl(this.blobEndpointUrl);
this.pipeline = pipeline;
this.storageClientContext = new StorageClientContext(
this.dfsEndpointUrl,
pipeline.toServiceClientOptions()
);
this.storageClientContextToBlobEndpoint = new StorageClientContext(
this.blobEndpointUrl,
pipeline.toServiceClientOptions()
);
this.isHttps = iEqual(getURLScheme(this.url) || "", "https");
this.credential = new AnonymousCredential();
for (const factory of this.pipeline.factories) {
if (
(isNode && factory instanceof StorageSharedKeyCredential) ||
factory instanceof AnonymousCredential
) {
this.credential = factory;
} else if (isTokenCredential((factory as any).credential)) {
// Only works if the factory has been attached a "credential" property.
// We do that in newPipeline() when using TokenCredential.
this.credential = (factory as any).credential;
}
}
// Override protocol layer's default content-type
const storageClientContext = this.storageClientContext as any;
storageClientContext.requestContentType = undefined;
const storageClientContextWithBlobEndpoint = this.storageClientContextToBlobEndpoint as any;
storageClientContextWithBlobEndpoint.requestContentType = undefined;
}
}