1.在VS中新建一个project,我起的名字是readFileFromAzure
2.在program.cs中编辑代码如下:
using Azure.Storage.Blobs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace readFileFromAzure
{
class Program
{
static void Main(string[] args)
{
string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; //blob connection string
string sourceContainerName = ConfigurationManager.AppSettings["sourcecontainerName"]; //source blob container name
var containerClient = new BlobContainerClient(connectionString, sourceContainerName);
string text;
using (var memoryStream = new MemoryStream())
{
containerClient.GetBlobClient("EASTestSuites/summary.csv").DownloadTo(memoryStream);
//puts the byte arrays to a string
text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
Console.WriteLine(text);
}
Console.ReadKey();
}
}
}
3. 需要在建立的project中安装以下Nuget Package
Azure.Storage.Blobs
安装方法:
(1)右键References,选择Manage Nuget Packages
(2)在Browse中输入要安装的package,点击安装
4.App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<add key="StorageConnectionString" value="这里的内容要从azure上copy" />
<add key="sourcecontainerName" value="container的名称" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
ConnectionString:
5. 我要读取的文件的blobName是EASTestSuites/summary.csv,之后将csv文件中的内容转成了string类型,可以在控制台中看到csv文件中的内容
using (var memoryStream = new MemoryStream())
{
containerClient.GetBlobClient("EASTestSuites/summary.csv").DownloadTo(memoryStream);
//puts the byte arrays to a string
text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
Console.WriteLine(text);
}
6. 因为读取到的文件是string类型,没有办法去对文件做一些处理,如果我们想统计文件中的一些数据,可以看下篇文章。