PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Sources/CsvReader.Azure/AzureExtensions.cs

https://github.com/tpwalke2/DataTable
C# | 117 lines | 76 code | 13 blank | 28 comment | 3 complexity | a0d768ef726410678563eb6d3b8a4857 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.WindowsAzure;
  6. using Microsoft.WindowsAzure.StorageClient;
  7. using System.IO;
  8. using System.Diagnostics;
  9. namespace DataAccess
  10. {
  11. public static class DataTableBuilderAzureExtensions
  12. {
  13. #if false
  14. public static MutableDataTable ReadFromAzureBlobLazy(this DataTableBuilder builder, CloudStorageAccount account, string containerName, string blobName)
  15. {
  16. CloudBlobContainer container = GetContainer(account, containerName);
  17. return ReadFromAzureBlobLazy(builder, container, blobName);
  18. }
  19. public static MutableDataTable ReadFromAzureBlobLazy(this DataTableBuilder builder, CloudBlobContainer container, string blobName)
  20. {
  21. }
  22. #endif
  23. /// <summary>
  24. /// Read a data table from azure blob. This will read the entire blob into memory and return a mutable data table.
  25. /// </summary>
  26. /// <param name="builder">builder</param>
  27. /// <param name="account">azure acount</param>
  28. /// <param name="containerName">conatiner name</param>
  29. /// <param name="blobName">blob name</param>
  30. /// <returns>in-memory mutable datatable from blob</returns>
  31. public static MutableDataTable ReadAzureBlob(this DataTableBuilder builder, CloudStorageAccount account, string containerName, string blobName)
  32. {
  33. CloudBlobContainer container = GetContainer(account, containerName);
  34. return ReadAzureBlob(builder, container, blobName);
  35. }
  36. /// <summary>
  37. /// Read a data table from azure blob. This will read the entire blob into memory and return a mutable data table.
  38. /// </summary>
  39. /// <param name="builder">builder</param>
  40. /// <param name="container">conatiner</param>
  41. /// <param name="blobName">blob name</param>
  42. /// <returns>in-memory mutable datatable from blob</returns>
  43. public static MutableDataTable ReadAzureBlob(this DataTableBuilder builder, CloudBlobContainer container, string blobName)
  44. {
  45. CloudBlob blob = container.GetBlobReference(blobName);
  46. if (!Exists(blob))
  47. {
  48. string containerName = container.Name;
  49. string accountName = container.ServiceClient.Credentials.AccountName;
  50. throw new FileNotFoundException(string.Format("container.blob {0}.{0} does not exist on the storage account '{2}'", containerName, blobName, accountName));
  51. }
  52. // We're returning a MutableDataTable (which is in-memory) anyways, so fine to download into an in-memory buffer.
  53. // Avoid downloading to a file because Azure nodes may not have a local file resource.
  54. string content = blob.DownloadText();
  55. var stream = new StringReader(content);
  56. var dt = DataTable.New.Read(stream);
  57. dt.Name = container.Name + "." + blobName;
  58. return dt;
  59. }
  60. internal static CloudBlobContainer GetContainer(CloudStorageAccount account, string containerName)
  61. {
  62. var client = account.CreateCloudBlobClient();
  63. var container = client.GetContainerReference(containerName);
  64. container.CreateIfNotExist();
  65. return container;
  66. }
  67. // Super lame that you need to check for exceptions. Seriously?
  68. // http://blog.smarx.com/posts/testing-existence-of-a-windows-azure-blob
  69. [DebuggerNonUserCode]
  70. private static bool Exists(CloudBlob blob)
  71. {
  72. try
  73. {
  74. blob.FetchAttributes();
  75. return true;
  76. }
  77. catch (StorageClientException e)
  78. {
  79. if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
  80. {
  81. return false;
  82. }
  83. else
  84. {
  85. throw;
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Read an Azure Table as a CSV. Returned CSV includes columns for the ParitionKey and RowKey.
  91. /// The row order is the same as Azure's natural row ordering (sorted by partition key, rowkey)
  92. /// This is a lazy function, so it reads the table rows at a time and does not read the entire table into memory.
  93. /// </summary>
  94. /// <param name="builder">builder</param>
  95. /// <param name="account">azure storage account</param>
  96. /// <param name="tableName">name of table within account</param>
  97. /// <returns></returns>
  98. public static DataTable ReadAzureTableLazy(this DataTableBuilder builder, CloudStorageAccount account, string tableName)
  99. {
  100. CloudTableClient tableClient = account.CreateCloudTableClient();
  101. return new AzureStreamingTable { _tableName = tableName, _tableClient = tableClient, Name = tableName };
  102. }
  103. }
  104. }