C#从Azure Blob Storage上读取csv文件

2 篇文章 0 订阅

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类型,没有办法去对文件做一些处理,如果我们想统计文件中的一些数据,可以看下篇文章。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值