当前位置:   article > 正文

C#从Azure Blob Storage上读取csv文件_c# azure storage 获取指定文件

c# azure storage 获取指定文件

1.在VS中新建一个project,我起的名字是readFileFromAzure

2.在program.cs中编辑代码如下:

  1. using Azure.Storage.Blobs;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace readFileFromAzure
  10. {
  11. class Program
  12. {
  13. static void Main(string[] args)
  14. {
  15. string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; //blob connection string
  16. string sourceContainerName = ConfigurationManager.AppSettings["sourcecontainerName"]; //source blob container name
  17. var containerClient = new BlobContainerClient(connectionString, sourceContainerName);
  18. string text;
  19. using (var memoryStream = new MemoryStream())
  20. {
  21. containerClient.GetBlobClient("EASTestSuites/summary.csv").DownloadTo(memoryStream);
  22. //puts the byte arrays to a string
  23. text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
  24. Console.WriteLine(text);
  25. }
  26. Console.ReadKey();
  27. }
  28. }
  29. }

3. 需要在建立的project中安装以下Nuget Package

Azure.Storage.Blobs

安装方法:

(1)右键References,选择Manage Nuget Packages

(2)在Browse中输入要安装的package,点击安装

4.App.config

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <startup>
  4. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  5. </startup>
  6. <appSettings>
  7. <add key="StorageConnectionString" value="这里的内容要从azure上copy" />
  8. <add key="sourcecontainerName" value="container的名称" />
  9. </appSettings>
  10. <runtime>
  11. <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  12. <dependentAssembly>
  13. <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  14. <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
  15. </dependentAssembly>
  16. <dependentAssembly>
  17. <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  18. <bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
  19. </dependentAssembly>
  20. <dependentAssembly>
  21. <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
  22. <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
  23. </dependentAssembly>
  24. <dependentAssembly>
  25. <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
  26. <bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
  27. </dependentAssembly>
  28. <dependentAssembly>
  29. <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
  30. <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
  31. </dependentAssembly>
  32. </assemblyBinding>
  33. </runtime>
  34. </configuration>

 ConnectionString:

5. 我要读取的文件的blobName是EASTestSuites/summary.csv,之后将csv文件中的内容转成了string类型,可以在控制台中看到csv文件中的内容

  1. using (var memoryStream = new MemoryStream())
  2.             {
  3.                 containerClient.GetBlobClient("EASTestSuites/summary.csv").DownloadTo(memoryStream);
  4.                 //puts the byte arrays to a string
  5.                 text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
  6.                 Console.WriteLine(text);
  7.             }

6. 因为读取到的文件是string类型,没有办法去对文件做一些处理,如果我们想统计文件中的一些数据,可以看下篇文章。

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

闽ICP备14008679号