当前位置:   article > 正文

C#连接字符串方式访问微软blob帮助类_c# azure blob

c# azure blob

Azure Blob 存储

Azure Blob 存储可帮助你创建数据湖以满足分析需求,并提供存储以构建功能强大的云原生和移动应用。通过分层存储你的长期数据优化成本,并且可灵活地纵向扩展高性能计算和机器学习工作负载。

Azure Blob 是唯一一种可为低延迟和交互式方案提供基于 SSD 的高级对象存储层的云存储服务

见官网定义

Nuget 下载 WindowsAzure.Storage包

  1. public class BlobHelper
  2. {
  3. private static CloudStorageAccount storageAccount = null;
  4. private static string filesharebase = "";
  5. private static CloudBlobClient blobClient = null;
  6. static BlobHelper()
  7. {
  8. string storageConnectionString = ConfigurationManager.AppSettings["storageconnectionstring"];
  9. if (!CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
  10. {
  11. throw new Exception("初始化blob fileshare异常");
  12. }
  13. blobClient = storageAccount.CreateCloudBlobClient();
  14. var url = blobClient.BaseUri.ToString();
  15. if (url.LastIndexOf("/").Equals(url.Length - 1))
  16. {
  17. url = url.Substring(0, url.Length - 1);
  18. }
  19. filesharebase = url;
  20. }
  21. /// <summary>
  22. /// 上传本地文件到blobfileshare
  23. /// </summary>
  24. /// <param name="localPath">本地路径</param>
  25. /// <param name="toPath">blob路径</param>
  26. public static void UploadFileToBlob(string localPath, string toPath)
  27. {
  28. UploadFile(toPath, localPath: localPath);
  29. }
  30. /// <summary>
  31. /// 上传文件流到blob
  32. /// </summary>
  33. /// <param name="stream"></param>
  34. /// <param name="streamLength"></param>
  35. /// <param name="toPath"></param>
  36. public static void UploadStreamToBlob(Stream stream, int streamLength, string toPath)
  37. {
  38. UploadFile(toPath, 2, stream: stream, length: streamLength);
  39. }
  40. /// <summary>
  41. /// 上传文件流到blob
  42. /// </summary>
  43. /// <param name="stream"></param>
  44. /// <param name="streamLength"></param>
  45. /// <param name="toPath"></param>
  46. public static void UploadByteToBlob(byte[] by, int length, string toPath)
  47. {
  48. UploadFile(toPath, 3, by: by, length: length);
  49. }
  50. /// <summary>
  51. /// 上传文件到blob 1物理路径上传 2文件流上传
  52. /// </summary>
  53. /// <param name="toPath"></param>
  54. /// <param name="uploadType"></param>
  55. /// <param name="localPath">本地物理路径</param>
  56. public static void UploadFile(string toPath, int uploadType = 1, string localPath = "", Stream stream = null, int length = 0, byte[] by = null)
  57. {
  58. var splitArrs = toPath.Split(new string[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries);
  59. CloudBlobContainer container = blobClient.GetContainerReference(splitArrs[0]);
  60. if (!container.Exists())
  61. {
  62. container.CreateAsync();
  63. }
  64. var splitLs = splitArrs.ToList();
  65. splitLs.RemoveAt(0);
  66. var blobpath = string.Join("/", splitLs.ToArray());
  67. CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(blobpath);
  68. if (uploadType == 1)
  69. {
  70. if (string.IsNullOrEmpty(localPath))
  71. {
  72. throw new Exception("上传类型为本地文件上传时,物理路径不能为空");
  73. }
  74. cloudBlockBlob.UploadFromFile(localPath);
  75. }
  76. else if (uploadType == 2)
  77. {
  78. if (stream == null || length == 0)
  79. {
  80. throw new Exception("上传类型为流上传时,流和长度不能为空");
  81. }
  82. cloudBlockBlob.UploadFromStream(stream, length);
  83. }
  84. else if (uploadType == 3)
  85. {
  86. if (by == null)
  87. {
  88. throw new Exception("二进制内容不能为空");
  89. }
  90. cloudBlockBlob.UploadFromByteArray(by, 0, length);
  91. }
  92. }
  93. /// <summary>
  94. /// 删除blob的文件
  95. /// </summary>
  96. /// <param name="blodPath">blob路径</param>
  97. public static void DeleteBlodFile(string blodPath)
  98. {
  99. var splitArrs = blodPath.Split(new string[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries);
  100. CloudBlobContainer container = blobClient.GetContainerReference(splitArrs[0]);
  101. if (!container.Exists())
  102. {
  103. return;
  104. }
  105. var splitLs = splitArrs.ToList();
  106. splitLs.RemoveAt(0);
  107. var blobpath = string.Join("/", splitLs.ToArray());
  108. CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(blobpath);
  109. if (cloudBlockBlob.Exists())
  110. {
  111. cloudBlockBlob.DeleteAsync();
  112. }
  113. }
  114. /// <summary>
  115. /// 资源url地址
  116. /// </summary>
  117. /// <param name="shareFilePath"></param>
  118. /// <returns></returns>
  119. public static string GetResourceUrlWithSas(string blobFilePath, string sasToken = "")
  120. {
  121. if (string.IsNullOrEmpty(sasToken))
  122. {
  123. var splitArrs = blobFilePath.Split(new string[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries);
  124. sasToken = GetAccountSASToken(splitArrs[0]);
  125. }
  126. return filesharebase + blobFilePath + sasToken;
  127. }
  128. /// <summary>
  129. /// 资源url地址
  130. /// </summary>
  131. /// <param name="shareFilePath"></param>
  132. /// <returns></returns>
  133. public static string GetResourceUrl(string blobFilePath)
  134. {
  135. return filesharebase + blobFilePath ;
  136. }
  137. /// <summary>
  138. /// 创建sastoken
  139. /// </summary>
  140. /// <param name="storageAccount"></param>
  141. /// <returns></returns>
  142. public static string GetAccountSASToken(string containerName, int expireMonth = 3)
  143. {
  144. CloudBlobContainer container = blobClient.GetContainerReference(containerName);
  145. SharedAccessBlobPolicy adHocPolicy = new SharedAccessBlobPolicy()
  146. {
  147. SharedAccessExpiryTime = DateTime.UtcNow.AddMonths(expireMonth),
  148. Permissions = SharedAccessBlobPermissions.Read
  149. };
  150. // Return the SAS token.
  151. return container.GetSharedAccessSignature(adHocPolicy, null);
  152. }
  153. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/138206
推荐阅读
相关标签
  

闽ICP备14008679号