/
Util.cs
154 lines (137 loc) · 6.35 KB
/
Util.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
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
//------------------------------------------------------------------------------
// <copyright file="Util.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation
// </copyright>
//------------------------------------------------------------------------------
namespace DataMovementSamples
{
using System;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Azure.Storage.File;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json.Linq;
/// <summary>
/// A helper class provides convenient operations against storage account configured in the App.config.
/// </summary>
public class Util
{
private static CloudStorageAccount storageAccount;
private static CloudBlobClient blobClient;
private static CloudFileClient fileClient;
/// <summary>
/// Get a CloudBlob instance with the specified name and type in the given container.
/// </summary>
/// <param name="containerName">Container name.</param>
/// <param name="blobName">Blob name.</param>
/// <param name="blobType">Type of blob.</param>
/// <returns>A <see cref="Task{T}"/> object of type <see cref="CloudBlob"/> that represents the asynchronous operation.</returns>
public static async Task<CloudBlob> GetCloudBlobAsync(string containerName, string blobName, BlobType blobType)
{
CloudBlobClient client = GetCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(containerName);
await container.CreateIfNotExistsAsync();
CloudBlob cloudBlob;
switch (blobType)
{
case BlobType.AppendBlob:
cloudBlob = container.GetAppendBlobReference(blobName);
break;
case BlobType.BlockBlob:
cloudBlob = container.GetBlockBlobReference(blobName);
break;
case BlobType.PageBlob:
cloudBlob = container.GetPageBlobReference(blobName);
break;
case BlobType.Unspecified:
default:
throw new ArgumentException(string.Format("Invalid blob type {0}", blobType.ToString()), "blobType");
}
return cloudBlob;
}
/// <summary>
/// Get a CloudBlobDirectory instance with the specified name in the given container.
/// </summary>
/// <param name="containerName">Container name.</param>
/// <param name="directoryName">Blob directory name.</param>
/// <returns>A <see cref="Task{T}"/> object of type <see cref="CloudBlobDirectory"/> that represents the asynchronous operation.</returns>
public static async Task<CloudBlobDirectory> GetCloudBlobDirectoryAsync(string containerName, string directoryName)
{
CloudBlobClient client = GetCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(containerName);
await container.CreateIfNotExistsAsync();
return container.GetDirectoryReference(directoryName);
}
/// <summary>
/// Get a CloudFile instance with the specified name in the given share.
/// </summary>
/// <param name="shareName">Share name.</param>
/// <param name="fileName">File name.</param>
/// <returns>A <see cref="Task{T}"/> object of type <see cref="CloudFile"/> that represents the asynchronous operation.</returns>
public static async Task<CloudFile> GetCloudFileAsync(string shareName, string fileName)
{
CloudFileClient client = GetCloudFileClient();
CloudFileShare share = client.GetShareReference(shareName);
await share.CreateIfNotExistsAsync();
CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();
return rootDirectory.GetFileReference(fileName);
}
/// <summary>
/// Delete the share with the specified name if it exists.
/// </summary>
/// <param name="shareName">Name of share to delete.</param>
public static async Task DeleteShareAsync(string shareName)
{
CloudFileClient client = GetCloudFileClient();
CloudFileShare share = client.GetShareReference(shareName);
await share.DeleteIfExistsAsync();
}
/// <summary>
/// Delete the container with the specified name if it exists.
/// </summary>
/// <param name="containerName">Name of container to delete.</param>
public static async Task DeleteContainerAsync(string containerName)
{
CloudBlobClient client = GetCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(containerName);
await container.DeleteIfExistsAsync();
}
private static CloudBlobClient GetCloudBlobClient()
{
if (Util.blobClient == null)
{
Util.blobClient = GetStorageAccount().CreateCloudBlobClient();
}
return Util.blobClient;
}
private static CloudFileClient GetCloudFileClient()
{
if (Util.fileClient == null)
{
Util.fileClient = GetStorageAccount().CreateCloudFileClient();
}
return Util.fileClient;
}
private static string LoadConnectionStringFromConfigration()
{
// How to create a storage connection string: http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
#if DOTNET5_4
//For .Net Core, will get Storage Connection string from Config.json file
return JObject.Parse(File.ReadAllText("Config.json"))["StorageConnectionString"].ToString();
#else
//For .net, will get Storage Connection string from App.Config file
return System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"];
#endif
}
private static CloudStorageAccount GetStorageAccount()
{
if (Util.storageAccount == null)
{
string connectionString = LoadConnectionStringFromConfigration();
Util.storageAccount = CloudStorageAccount.Parse(connectionString);
}
return Util.storageAccount;
}
}
}