-
Notifications
You must be signed in to change notification settings - Fork 0
/
IBlob.cs
395 lines (332 loc) · 14.9 KB
/
IBlob.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
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
// <copyright file="IBlob.cs" company="CodeRanger.com">
// CodeRanger.com and Dan Petitt. All rights reserved.
// </copyright>
// <author>Dan Petitt</author>
namespace AzureStorage
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
[Guid("BCC86C64-8A61-47F4-B653-0A95A031A137")]
/// <summary>
/// Provides COM interface to our Blob class for managing a blob in Azure Storage.
/// <para />
/// Call Initialize first to setup the Azure account, then you can use the other methods to perform actions on your Storage containers
/// </summary>
public interface IBlob
{
/// <summary>
/// Gets or sets the account name for an Azure Storage account, not needed if UseDevelopmentStorage field is set
/// </summary>
[DispId(1)]
string AccountName { get; set; }
/// <summary>
/// Gets or sets the account key for an Azure Storage account, not needed if UseDevelopmentStorage field is set
/// </summary>
[DispId(2)]
string AccountKey { get; set; }
/// <summary>
/// Gets or sets a value indicating whether to use local development storage instead of an Azure Storage account
/// </summary>
[DispId(3)]
bool UseDevelopmentStorage { get; set; }
/// <summary>
/// Initializes the blob storage account with the given account settings
/// </summary>
[DispId(4)]
void Initialize();
/// <summary>
/// Allows you to upload a block blob to Azure storage using account settings when initialized
/// </summary>
/// <param name="containerName">The container name to use or automatically create for containing the blob uploaded</param>
/// <param name="fullPathToFileForUpload">The local file path of the file to upload</param>
/// <param name="fileType">Optional file mime-type, for example application/ms-excel for Excel file, defaults to application/x-octet-stream if not provided</param>
/// <returns>The URI of the blob in Azure Storage</returns>
[DispId(5)]
string UploadBlockBlob( string containerName, string fullPathToFileForUpload, string fileType = "application/x-octet-stream" );
/// <summary>
/// Returns an array of blobs in given container
/// </summary>
/// <param name="containerName">The container name to use or automatically create for investigating</param>
/// <returns>An array of blob URIs in the given container</returns>
[DispId(6)]
string GetBlobsInContainer( string containerName );
/// <summary>
/// Downloads a blob with the given complete URI to a local file path
/// </summary>
/// <param name="uri">Blob URI which you wish to delete</param>
/// <param name="pathToFileForDownload">Local path in which the blob is saved. This is the path and does not include the filename which is automatically added with the blob name</param>
[DispId(7)]
void DownloadBlob( string uri, string pathToFileForDownload );
/// <summary>
/// Deletes a blob with a given complete URI
/// </summary>
/// <param name="uri">Blob URI which you wish to delete</param>
[DispId(8)]
void DeleteBlob( string uri );
}
[ClassInterface( ClassInterfaceType.None )]
[ComVisible( true )]
/// <summary>
/// Class interface for managing a blob in Azure Storage
/// </summary>
public class Blob : IBlob
{
/// <summary>
/// Private property to store the current Azure storage account created on Initialize
/// </summary>
[SuppressMessage( "StyleCop.CSharp.NamingRules", "SA1309:FieldNamesMustNotBeginWithUnderscore", Justification = "Reviewed." )]
private CloudStorageAccount _storageAccount;
/// <summary>
/// Private property to store the account key for an Azure Storage account
/// </summary>
[SuppressMessage( "StyleCop.CSharp.NamingRules", "SA1309:FieldNamesMustNotBeginWithUnderscore", Justification = "Reviewed." )]
private string _storageAccountKey = string.Empty;
/// <summary>
/// Private property to store the account name for an Azure Storage account
/// </summary>
[SuppressMessage( "StyleCop.CSharp.NamingRules", "SA1309:FieldNamesMustNotBeginWithUnderscore", Justification = "Reviewed." )]
private string _storageAccountName = string.Empty;
/// <summary>
/// Private property to store the value for using development storage instead of an Azure Storage account
/// </summary>
[SuppressMessage( "StyleCop.CSharp.NamingRules", "SA1309:FieldNamesMustNotBeginWithUnderscore", Justification = "Reviewed." )]
private bool _storageUseDevelopmentStorage = false;
/// <summary>
/// Gets or sets the account name for an Azure Storage account, not needed if UseDevelopmentStorage field is set
/// </summary>
public string AccountName
{
get
{
return this._storageAccountName;
}
set
{
this._storageAccountName = value;
}
}
/// <summary>
/// Gets or sets the account key for an Azure Storage account, not needed if UseDevelopmentStorage field is set
/// </summary>
public string AccountKey
{
get
{
return this._storageAccountKey;
}
set
{
this._storageAccountKey = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether to use local development storage instead of an Azure Storage account
/// </summary>
public bool UseDevelopmentStorage
{
get
{
return this._storageUseDevelopmentStorage;
}
set
{
this._storageUseDevelopmentStorage = value;
}
}
/// <summary>
/// Initializes the blob storage account with the account settings set by the AccountName and AccountKey fields
/// </summary>
public void Initialize()
{
if( !this.UseDevelopmentStorage )
{
if( this.AccountName.Length == 0 )
{
throw new COMException( "Error initializing the Azure Storage Account because accountName parameter is empty" );
}
else if( this.AccountKey.Length == 0 )
{
throw new COMException( "Error initializing the Azure Storage Account because accountKey parameter is empty" );
}
}
try
{
this._storageAccount = this.UseDevelopmentStorage ? CloudStorageAccount.DevelopmentStorageAccount : new CloudStorageAccount( new StorageCredentials( this.AccountName, this.AccountKey ), true );
}
catch( System.Exception ex )
{
throw new COMException( "Error initializing the Azure Storage Account because: " + ex.Message );
}
}
/// <summary>
/// Allows you to upload a block blob to Azure storage using account settings when initialized
/// </summary>
/// <param name="containerName">The container name to use or automatically create for containing the blob uploaded</param>
/// <param name="fullPathToFileForUpload">The local file path of the file to upload</param>
/// <param name="fileType">Optional file mime-type, for example application/ms-excel for Excel file, defaults to application/x-octet-stream if not provided</param>
/// <returns>The URI of the blob in Azure Storage</returns>
public string UploadBlockBlob( string containerName, string fullPathToFileForUpload, string fileType = "application/x-octet-stream" /*, bool isPublic*/ )
{
try
{
Console.WriteLine( "> Uploading BlockBlob" );
// Create the blob client
CloudBlobClient blobClient = this._storageAccount.CreateCloudBlobClient();
// create or find our container references
CloudBlobContainer container = GetContainer( blobClient, containerName, new BlobContainerPermissions{ PublicAccess = BlobContainerPublicAccessType.Blob } );
// Get our block reference and upload data from the given filepath
CloudBlockBlob blockBlob = container.GetBlockBlobReference( Path.GetFileName( fullPathToFileForUpload ) );
blockBlob.Properties.ContentType = fileType;
using( var filestream = File.OpenRead( fullPathToFileForUpload ) )
{
blockBlob.UploadFromStream( filestream );
}
Console.WriteLine( "\t Blob is now available at {0}", blockBlob.Uri.ToString() );
return blockBlob.Uri.ToString();
}
catch( System.Exception ex )
{
throw new COMException( "Error uploading block blob because: " + ex.Message );
}
}
/// <summary>
/// Returns an array of blobs in given container
/// </summary>
/// <param name="containerName">The container name to use or automatically create for investigating</param>
/// <returns>An array of blob URIs in the given container</returns>
public string GetBlobsInContainer( string containerName )
{
Console.WriteLine( "> List Blobs" );
try
{
CloudBlobClient blobClient = this._storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference( containerName );
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter( sb );
using( JsonWriter writer = new JsonTextWriter( sw ) )
{
writer.Formatting = Formatting.None;
writer.WriteStartArray();
foreach( IListBlobItem blob in container.ListBlobs( null, false ) )
{
// Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
// use blob.GetType() and cast to appropriate type to gain access to properties specific to each type
Console.WriteLine( "\t {0} {1} \t {2}", blob.GetType(), Environment.NewLine, blob.Uri );
if( blob.GetType() == typeof( CloudBlockBlob ) )
{
writer.WriteStartObject();
CloudBlockBlob cloudBlobItem = new CloudBlockBlob( blob.Uri );
cloudBlobItem.FetchAttributes();
writer.WritePropertyName( "name" );
writer.WriteValue( cloudBlobItem.Name );
writer.WritePropertyName( "uri" );
writer.WriteValue( cloudBlobItem.Uri.ToString() );
writer.WritePropertyName( "type" );
writer.WriteValue( cloudBlobItem.Properties.BlobType.ToString() );
writer.WritePropertyName( "lastModified" );
writer.WriteValue( cloudBlobItem.Properties.LastModified.ToString() );
writer.WritePropertyName( "contentType" );
writer.WriteValue( cloudBlobItem.Properties.ContentType );
writer.WritePropertyName( "contentMD5" );
writer.WriteValue( cloudBlobItem.Properties.ContentMD5.ToString() );
writer.WritePropertyName( "size" );
writer.WriteValue( cloudBlobItem.Properties.Length.ToString() );
writer.WriteEndObject();
}
}
writer.WriteEndArray();
}
return sb.ToString();
}
catch( System.Exception ex )
{
throw new COMException( "Error getting container blobs because: " + ex.Message );
}
// foreach( IListBlobItem blob in container.ListBlobs( null, true ) )
// {
// //Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
// // use blob.GetType() and cast to appropriate type to gain access to properties specific to each type
// Console.WriteLine( "\t {0} {1} \t {2}", blob.GetType(), Environment.NewLine, blob.Uri );
// }
}
/// <summary>
/// Downloads a blob with the given complete URI to a local file path
/// </summary>
/// <param name="uri">Blob URI which you wish to delete</param>
/// <param name="pathToFileForDownload">Local file path in which the blob is saved</param>
public void DownloadBlob( string uri, string pathToFileForDownload )
{
Console.WriteLine( "> Download Blob from {0}", uri.ToString() );
try
{
CloudBlobClient blobClient = this._storageAccount.CreateCloudBlobClient();
ICloudBlob blobItem = blobClient.GetBlobReferenceFromServer( new Uri( uri ) );
string downloadToPath = string.Format( "{0}/{1}", pathToFileForDownload, blobItem.Name );
using( var fs = File.OpenWrite( downloadToPath ) )
{
blobItem.DownloadToStream( fs );
Console.WriteLine( "\t Blob downloaded to file: {0}", downloadToPath );
}
// //Demonstrate how to download a blob from uri to a MemoryStream
// using ( var ms = new MemoryStream() )
// {
// blobItem.DownloadToStream( ms );
//
// //Now process the memory stream however you like
// Console.WriteLine( "\t Now process the memory stream however you like. Memory Stream Length: {0}", ms.Length );
// }
}
catch( System.Exception ex )
{
throw new COMException( "Error downloading blob " + uri + " to " + pathToFileForDownload + " because: " + ex.Message );
}
}
/// <summary>
/// Deletes a blob with a given complete URI
/// </summary>
/// <param name="uri">Blob URI which you wish to delete</param>
public void DeleteBlob( string uri )
{
Console.WriteLine( "> Delete Blob" );
try
{
CloudBlobClient blobClient = this._storageAccount.CreateCloudBlobClient();
ICloudBlob blobItem = blobClient.GetBlobReferenceFromServer( new Uri( uri ) );
if( blobItem != null )
{
var success = blobItem.DeleteIfExists();
Console.WriteLine( "\t {0} Deleting Blob {1}", success ? "Successful" : "Unsuccessful", uri.ToString() );
}
}
catch( System.Exception ex )
{
throw new COMException( "Error deleting blob " + uri + " because: " + ex.Message );
}
}
/// <summary>
/// Private method to get a container reference from a container name. If the container does not exist, it is created with the given permissions.
/// </summary>
/// <param name="blobClient">Reference to a CloudBlobClient for this storage account</param>
/// <param name="containerName">The container name to use or automatically create</param>
/// <param name="permissions">Permissions object to use if the container needs to be created</param>
/// <returns>A reference to the existing or new storage container</returns>
private static CloudBlobContainer GetContainer( CloudBlobClient blobClient, string containerName, BlobContainerPermissions permissions )
{
Console.WriteLine( "> Create Container '{0}' and Set Permissions to {1}", containerName, permissions.ToString() );
// Retrieve a reference to a container
CloudBlobContainer container = blobClient.GetContainerReference( containerName.ToLower() );
// Create the container if it doesn't already exist
container.CreateIfNotExists();
// Set the container permissions
container.SetPermissions( permissions );
return container;
}
}
}