-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathStorageServiceClient.cs
86 lines (70 loc) · 2.43 KB
/
StorageServiceClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using RESTClient;
using System;
using System.Text.RegularExpressions;
namespace Azure.StorageServices {
public sealed class StorageServiceClient : RestClient {
private string account;
public string Account {
get {
return account;
}
}
private byte[] key;
public byte[] Key {
get {
return key;
}
}
// https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/versioning-for-the-azure-storage-services
private string version;
public string Version {
get {
return version;
}
}
public StorageServiceClient(string url, string accessKey, string version = "2017-04-17", string account = "") : base(url) {
this.version = version;
if (!string.IsNullOrEmpty(accessKey)) {
this.key = Convert.FromBase64String(accessKey);
}
if (!string.IsNullOrEmpty(account)) {
this.account = account;
} else {
this.account = GetAccountName(url);
}
}
/// <summary>
/// Creates a new instance of the <see cref="Unity3dAzure.StorageServices.StorageServiceClient"/> class.
/// </summary>
/// <param name="account">Storage account name.</param>
/// <param name="accessKey">Access key.</param>
public static StorageServiceClient Create(string account, string accessKey, string version = "2017-04-17") {
string url = GetPrimaryEndpoint(account);
return new StorageServiceClient(url, accessKey, version, account);
}
public BlobService GetBlobService() {
return new BlobService(this);
}
public string PrimaryEndpoint() {
return GetPrimaryEndpoint(account);
}
public string SecondaryEndpoint() {
return GetSecondaryEndpoint(account);
}
private static string GetPrimaryEndpoint(string account) {
return "https://" + account + ".blob.core.windows.net/";
}
private static string GetSecondaryEndpoint(string account) {
return "https://" + account + ".blob.core.windows.net/";
}
private string GetAccountName(string url) {
var match = Regex.Match(url, @"^https?:\/\/([a-z0-9]+)", RegexOptions.IgnoreCase);
if (match.Groups.Count == 2 && match.Groups[1].Value.Length > 0) {
return match.Groups[1].Value;
}
return url;
}
}
}