C# Azure Blob Storage Manager Class
Here is a C# Azure Blob Storage Manager Class which I hope you find useful as I’ve needed it often enough. It provides basic functionality for blob storage in the Azure cloud. Sample usage included within the class level comments.
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 |
//C# Azure Blob Storage Manager class. //Basic functionality for blob storage in the Azure cloud. using Microsoft.WindowsAzure.Storage; using Microsoft.WindowsAzure.Storage.Auth; using Microsoft.WindowsAzure.Storage.Blob; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace YourNameSpace { #region Sample Use /******************************************************************************** * Code adapted from * http://dotnetspeak.com/2012/08/uploading-directory-to-azure-blob-storage * * Sample usage: AzureBlobManager abm = new AzureBlobManager(); abm.ContainerName = AzureBlobManager.ROOT_CONTAINER_NAME; abm.DirectoryName = "TheBlob" + "/" + "PathYouWant" + "/"; //Check if the Container Exists if (abm.DoesContainerExist(abm.ContainerName)) { //If so, do this if you want to delete all the files already there. if (abm.DoesBlobDirectoryExist(abm.ContainerName, abm.DirectoryName)) { abm.DeleteBlobDirectory(abm.ContainerName, abm.DirectoryName); } } else { throw new Exception("The specified Azure Container \"" + abm.ContainerName + "\" doesn't exist."); } //For example purposes, using a List of object arrays which holds data from a database List<object[]> SQLDataByCols = new List<object[]>(); // : //TODO: populate SQLDataByCols with data // : //Upload multiple blobs in Parallel Parallel.For(0, SQLDataByCols.Count, x => //foreach (object[] Col in SQLDataByCols) //use this if you want it to be single threaded instead { object[] Col = SQLDataByCols[x]; Guid guid = Guid.NewGuid(); //Next line uploads the data, compressed, with just a Guid for the file name. //You can obviously use whatever name you want. //For cloud security purposes, it's a lot harder to figure out what a file is for if it's given a GUID as a name. //You don't have to compress your data. Just so long as it's passed as a byte[]. //Just remember to decompress when you download again. abm.PutBlob(abm.ContainerName, abm.DirectoryName + guid + ".dat", Utilities.Zip(JsonConvert.SerializeObject(Col))); } //for loop ); //parallel * * *********************************************************************************/ #endregion Sample Use /// <summary> /// Blob storage manager class. Basic functionality for blob storage in the Azure cloud. /// </summary> public class AzureBlobManager { #region Public Constants /// <summary> /// The root top level container in Azure blob. /// Rename as appropriate for your system. /// </summary> public const string ROOT_CONTAINER_NAME = "TopLevelContainer"; #endregion Public Constants #region Private Members private CloudBlobClient _blobClient; private string _containerName; private string _blobName; private string _directoryName; //To initialize the default storage credentials if none are provided. //For now we're going to assume everything is going to this blob storage. private StorageCredentials _storageCredentials = new StorageCredentials("your_login", "the_password_from_azure"); private CloudStorageAccount _storageAccount;// = new CloudStorageAccount(_storageCredentials, false); private CloudBlobContainer _container; private CloudBlockBlob _blockBlob; // = _container.GetBlockBlobReference("myfirstupload.txt"); #endregion Private Members #region Constructors /// <summary> /// Initializes a new instance of the <see cref="AzureBlobManager" /> class /// with the default values for the storage credentials /// and the associated containername as specified by the private members. /// </summary> public AzureBlobManager() { _storageAccount = new CloudStorageAccount(_storageCredentials, false); _blobClient = _storageAccount.CreateCloudBlobClient(); _container = _blobClient.GetContainerReference(ROOT_CONTAINER_NAME); } /// <summary> /// Initializes a new instance of the <see cref="AzureBlobManager" /> class /// with the specified container and storage credentials. /// </summary> public AzureBlobManager(string containerName, StorageCredentials storageCredentials) { _storageAccount = new CloudStorageAccount(storageCredentials, false); _blobClient = _storageAccount.CreateCloudBlobClient(); _containerName = containerName; _container = _blobClient.GetContainerReference(_containerName); } #endregion Constructors #region Public Properties /// <summary> /// Gets/sets the name of the Azure Container /// </summary> public string ContainerName { get { return _containerName; } set { _containerName = value; } } /// <summary> /// Gets/sets the name of the Blob /// </summary> public string BlobName { get { return _blobName; } set { _blobName = value; } } /// <summary> /// Gets/sets the "virtual" directory. /// </summary> public string DirectoryName { get { return _directoryName; } set { _directoryName = value; } } #endregion Public Properties #region Public Methods /// <summary> /// Downloads the contents of a blob into a byte[] /// </summary> /// <param name="containerName">The blob's container</param> /// <param name="blobName">The name of the blob to download</param> /// <returns>byte[] with the blob's contents</returns> public byte[] GetBlob(string containerName, string blobName) { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); _blockBlob = container.GetBlockBlobReference(blobName); _blockBlob.FetchAttributes(); long fileByteLength = _blockBlob.Properties.Length; byte[] fileContents = new byte[fileByteLength]; _blockBlob.DownloadToByteArray(fileContents, 0); return fileContents; } /// <summary> /// Updates or created a blob in Azure blob storage /// </summary> /// <param name="containerName">Name of the container to upload into.</param> /// <param name="blobName">Name of the blob.</param> /// <param name="content">The content of the blob.</param> /// <returns></returns> public bool PutBlob(string containerName, string blobName, byte[] content) { return ExecuteWithExceptionHandlingAndReturnValue( () => { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); _blockBlob = container.GetBlockBlobReference(blobName); //CloudBlob blob = container.GetBlobReference(blobName); using (var stream = new MemoryStream(content, writable: false)) { _blockBlob.UploadFromStream(stream); } //blob.UploadByteArray(content); }); } /// <summary> /// Creates the container in Azure blob storage. /// This really shouldn't need to happen once a system has been established. /// Should call DoesContainerExist to see if it exists before calling this method. /// </summary> /// <param name="containerName">Name of the container.</param> /// <returns>True if container was created successfully; false otherwise</returns> public bool CreateContainer(string containerName) { return ExecuteWithExceptionHandlingAndReturnValue( () => { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); container.Create(); }); } /// <summary> /// Deletes the container in Azure blob storage. /// Better make sure you have backups before you call this. :-) /// </summary> /// <param name="containerName">Name of the container.</param> /// <returns>True if contianer was deleted successfully; false otherwise.</returns> public bool DeleteContainer(string containerName) { return ExecuteWithExceptionHandlingAndReturnValue( () => { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); container.Delete(); }); } /// <summary> /// Checks if a container exists. /// </summary> /// <param name="containerName">Name of the container.</param> /// <returns>True if container exists; false otherwise</returns> public bool DoesContainerExist(string containerName) { bool returnValue = false; ExecuteWithExceptionHandling( () => { IEnumerable<CloudBlobContainer> containers = _blobClient.ListContainers(); returnValue = containers.Any(one => one.Name == containerName); }); return returnValue; } /// <summary> /// Checks if a blob exists. /// </summary> /// <param name="containerName">Name of the container.</param> /// <param name="blobName">Name of the blob.</param> /// <returns>True if blob exists; false otherwise</returns> public bool DoesBlobExist(string containerName, string blobName) { bool returnValue = false; ExecuteWithExceptionHandling( () => { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); _blockBlob = container.GetBlockBlobReference(blobName); returnValue = _blockBlob.Exists(); }); return returnValue; } /// <summary> /// Checks if a blob (virtual) directory exists. /// </summary> /// <param name="containerName">Name of the container.</param> /// <param name="directoryName">Name of the directory.</param> /// <returns>True if the directory exists; false otherwise.</returns> public bool DoesBlobDirectoryExist(string containerName, string directoryName) { bool returnValue = false; ExecuteWithExceptionHandling( () => { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); returnValue = container.ListBlobs(directoryName, false).ToArray().Length > 0; }); return returnValue; } /// <summary> /// Moves a Blob directory from one location to another within the same container. /// If the target directory exists prior to the copy, it will be deleted first. /// </summary> /// <param name="containerName">The blob container to move to. If the container does not exist it will be created.</param> /// <param name="sourceDirectory">The source blob directory to copy the blobs from. Should end with a "/" character.</param> /// <param name="targetDirectory">The target blob directory to copy the blobs to. Should end with a "/" character. If it /// already exists, it will be deleted, along with any blobs already contained within.</param> /// <returns>true indicating success; false otherwise.</returns> public bool MoveBlobDirectory(string containerName, string sourceDirectory, string targetDirectory) { bool returnValue = true; ExecuteWithExceptionHandling( () => { //Check the container exists. If not, create it. if (!DoesContainerExist(containerName)) { CreateContainer(containerName); } CloudBlobContainer container = _blobClient.GetContainerReference(containerName); //see if the target directory exists if (DoesBlobDirectoryExist(containerName, targetDirectory)) { //if so, see if we can delete it if (!DeleteBlobDirectory(containerName, targetDirectory)) returnValue = false; } if (returnValue) //no issues thus far. That's good. { //copy the files over in parallel Parallel.ForEach(container.ListBlobs(sourceDirectory, false), item => //foreach (IListBlobItem item in container.ListBlobs(sourceDirectory, false)) { //These are declared inside the loop for multi-threading purposes. string sourceBlobName = string.Empty; string targetBlobName = string.Empty; CloudBlockBlob sourceBlob; CloudBlockBlob targetBlob; try { if (item.GetType() == typeof(CloudBlob) || item.GetType().BaseType == typeof(CloudBlob)) { sourceBlobName = ((CloudBlob)item).Name; sourceBlob = container.GetBlockBlobReference(sourceBlobName); //Set the new blob name, replacing the prefix with an //empty string to just get the name of the file. //The prefix contains the virtual folder path. targetBlobName = ((CloudBlob)item).Name.Replace(((CloudBlob)item).Parent.Prefix, String.Empty); targetBlob = container.GetBlockBlobReference(targetDirectory + targetBlobName); targetBlob.StartCopy(sourceBlob); DeleteBlob(containerName, sourceBlobName); } } catch (Exception e) { //Something happened, so assume not successful. returnValue = false; } } //for loop ); //parallel } }); return returnValue; } /// <summary> /// Deletes the specified blob from the specified container if it exists. /// </summary> /// <param name="containerName">The name of the Container</param> /// <param name="blobName">The blob "file" to delete.</param> /// <returns></returns> public bool DeleteBlob(string containerName, string blobName) { return ExecuteWithExceptionHandlingAndReturnValue( () => { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); _blockBlob = container.GetBlockBlobReference(blobName); _blockBlob.Delete(); }); } /// <summary> /// Removes a "virtual" blob directory. For a directory to be removed, /// every single blob file must also be removed. /// So this will remove all the blobs within this directory. Make sure that's what you want! /// </summary> /// <param name="containerName">The container containing the directory.</param> /// <param name="directoryName">The virtual directory to remove.</param> /// <returns></returns> public bool DeleteBlobDirectory(string containerName, string directoryName) { return ExecuteWithExceptionHandlingAndReturnValue( () => { CloudBlobContainer container = _blobClient.GetContainerReference(containerName); Parallel.ForEach(container.ListBlobs(directoryName, false), item => //foreach (IListBlobItem item in container.ListBlobs(directoryName,false)) { if (item.GetType() == typeof(CloudBlob) || item.GetType().BaseType == typeof(CloudBlob)) { ((CloudBlob)item).Delete(); } } ); }); //return true; } /// <summary> /// Uploads the directory to blobl storage /// </summary> /// <param name="sourceDirectory">The source directory name.</param> /// <param name="containerName">Name of the container to upload to.</param> /// <returns>True if successfully uploaded</returns> public bool UploadDirectory(string sourceDirectory, string containerName) { return UploadDirectory(sourceDirectory, containerName, string.Empty); } /// <summary> /// Recursive function to upload files in a directory to an Azure blob directory. /// HASN'T BEEN FULLY TESTED. /// </summary> /// <param name="sourceDirectory">The directory containing all the files to be copied</param> /// <param name="containerName">The target Azure container</param> /// <param name="prefixAzureFolderName">The destination "folder" to copy everything to in Azure.</param> /// <returns>true indicating success; false otherwise.</returns> private bool UploadDirectory(string sourceDirectory, string containerName, string prefixAzureFolderName) { return ExecuteWithExceptionHandlingAndReturnValue( () => { if (!DoesContainerExist(containerName)) { CreateContainer(containerName); } var folder = new DirectoryInfo(sourceDirectory); FileInfo[] files = folder.GetFiles(); string blobName = String.Empty; foreach (var fileInfo in files) { blobName = fileInfo.Name; if (!string.IsNullOrEmpty(prefixAzureFolderName)) { blobName = prefixAzureFolderName + "/" + blobName; } PutBlob(containerName, blobName, File.ReadAllBytes(fileInfo.FullName)); } DirectoryInfo[] subFolders = folder.GetDirectories(); string prefix = String.Empty; foreach (var directoryInfo in subFolders) { prefix = directoryInfo.Name; if (!string.IsNullOrEmpty(prefixAzureFolderName)) { prefix = prefixAzureFolderName + "/" + prefix; } UploadDirectory(directoryInfo.FullName, containerName, prefix); } }); } #endregion Public Methods #region Private Methods //A good read on storage exceptions: //http://stackoverflow.com/questions/15623306/error-handling-for-windows-azure-storage-migration-from-1-7-to-2-0 //and //https://alexandrebrisebois.wordpress.com/2013/07/03/handling-windows-azure-storage-exceptions/ /// <summary> /// Tries the perform the Action. If an exception occurs that is not a "409" error, the /// exception will be rethrown. Blob service error codes: https://msdn.microsoft.com/en-au/library/azure/dd179439.aspx /// </summary> /// <param name="action">The Action to perform.</param> private void ExecuteWithExceptionHandling(Action action) { try { action(); } catch (StorageException ex) { //Blob service error codes: https://msdn.microsoft.com/en-au/library/azure/dd179439.aspx //Ignore lease already present error if (ex.RequestInformation.ExtendedErrorInformation.ErrorCode != "409") { throw; } } } /// <summary> /// Tries to perform the specified Action. An exception is thrown that is not a "409" exception. /// Blob service error codes: https://msdn.microsoft.com/en-au/library/azure/dd179439.aspx /// </summary> /// <param name="action">The Action to perform.</param> /// <returns>True if no exceptions or only a 409 error; false otherwise.</returns> private bool ExecuteWithExceptionHandlingAndReturnValue(Action action) { try { action(); return true; } catch (StorageException ex) { //Blob service error codes: https://msdn.microsoft.com/en-au/library/azure/dd179439.aspx //Ignore lease already present error if (ex.RequestInformation.ExtendedErrorInformation.ErrorCode != "409") { return false; } throw; } } #endregion Private Methods } } |