C# 无法从Azure函数中的Blob存储中读取文件

C# 无法从Azure函数中的Blob存储中读取文件,c#,.net,azure,azure-functions,azure-storage-blobs,C#,.net,Azure,Azure Functions,Azure Storage Blobs,我正在尝试使用Azure Function应用程序从Blob存储读取文本文件。我的目标是读取CSV文件,并将其重新格式化为新的CSV,添加原始CSV文件中没有的其他详细信息 我不断收到以下编译错误: 2018-11-28T00:22:34.125 [Error] run.csx(60,19): error CS1061: 'CloudBlockBlob' does not contain a definition for 'DownloadToStream' and no extension m

我正在尝试使用Azure Function应用程序从Blob存储读取文本文件。我的目标是读取CSV文件,并将其重新格式化为新的CSV,添加原始CSV文件中没有的其他详细信息

我不断收到以下编译错误:

2018-11-28T00:22:34.125 [Error] run.csx(60,19): error CS1061: 'CloudBlockBlob' does not contain a definition for 'DownloadToStream' and no extension method 'DownloadToStream' accepting a first argument of type 'CloudBlockBlob' could be found (are you missing a using directive or an assembly reference?) 
我可以将代码的Blob存储部分复制到控制台应用程序的项目中,它可以很好地编译

我是不是漏掉了一封推荐信

这是完整函数减去Blob存储的连接字符串

#r "Newtonsoft.Json"

#r "System.Configuration"
#r "System.Data"

#r "System.Collections"
#r "System.IO.Compression"
#r "System.Net"
#r "Microsoft.WindowsAzure.Storage"
#r "System.Linq"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

using System;
using System.Configuration;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Linq;
using System.Threading.Tasks;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string filePath = req.Query["filePath"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    filePath = filePath ?? data?.filePath;

    var fileInfo = GetFileInfo(filePath);

    string line = "";

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("Connection String goes Here");

        CloudBlobClient client = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = client.GetContainerReference(fileInfo.Container);

        var fileNameWithFolder =
                    fileInfo.DirectoryName == ""
                        ? fileInfo.FileName
                        : $"{fileInfo.DirectoryName}/{fileInfo.FileName}";

        CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(fileNameWithFolder);

        using (var memoryStream = new MemoryStream())
        {
            try
            {
                blockBlob2.DownloadToStream(memoryStream);
                line = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                line = ex.Message;
            }
        }   

    return filePath != null
        ? (ActionResult)new OkObjectResult($"filePath: {filePath} Container: {fileInfo.Container} DirectoryName: {fileInfo.DirectoryName} FileName: {fileInfo.FileName}*********{line}")
        : new BadRequestObjectResult("Please pass a filePath on the query string or in the request body");
}

private static FileInfo GetFileInfo(string filePath)
{
    int index = filePath.IndexOf("/");
    filePath = (index < 0)
        ? filePath
        : filePath.Remove(index, 1);

    var filePathSplit = filePath.Split('/');
    var fileInfo = new FileInfo();
    fileInfo.Container = filePathSplit[0];

    if ((filePathSplit.Length - 2) > 0)
    {
        var folderName = "";

        for(var i = 1; i < filePathSplit.Length - 1; i++)
        {
            if (folderName.Trim().Length > 0)
            {
                folderName += "/";
            }

            folderName += filePathSplit[i];
        }

        fileInfo.DirectoryName = folderName;
    }

    fileInfo.FileName = filePathSplit[filePathSplit.Length - 1];

    return fileInfo;
}

public class FileInfo
{
    public string Container { get; set; }
    public string DirectoryName { get; set; }
    public string FileName { get; set; }
}
#r“Newtonsoft.Json”
#r“系统配置”
#r“系统数据”
#r“系统集合”
#r“系统IO压缩”
#r“System.Net”
#r“Microsoft.WindowsAzure.Storage”
#r“System.Linq”
Net系统;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Primitives;
使用Newtonsoft.Json;
使用制度;
使用系统配置;
使用系统文本;
使用System.IO;
使用系统IO压缩;
使用System.Data.SqlClient;
使用System.Collections.Generic;
使用系统数据;
使用Microsoft.WindowsAzure.Storage;
使用Microsoft.WindowsAzure.Storage.Blob;
使用System.Linq;
使用System.Threading.Tasks;
公共静态异步任务运行(HttpRequest请求,ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
字符串filePath=req.Query[“filePath”];
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
动态数据=JsonConvert.DeserializeObject(requestBody);
filePath=filePath??data?.filePath;
var fileInfo=GetFileInfo(filePath);
字符串行=”;
CloudStorageAccount-storageAccount=CloudStorageAccount.Parse(“连接字符串在此显示”);
CloudBlobClient=storageAccount.CreateCloudBlobClient();
CloudBlobContainer container=client.GetContainerReference(fileInfo.container);
var fileNameWithFolder=
fileInfo.DirectoryName==“”
?fileInfo.FileName
:$“{fileInfo.DirectoryName}/{fileInfo.FileName}”;
CloudBlockBlob blockBlob2=container.GetBlockBlobReference(fileNameWithFolder);
使用(var memoryStream=new memoryStream())
{
尝试
{
blockBlob2.下载到流(memoryStream);
line=System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
}
捕获(例外情况除外)
{
行=例如消息;
}
}   
返回文件路径!=null
?(ActionResult)新的OkObjectResult($“文件路径:{filePath}容器:{fileInfo.Container}目录名:{fileInfo.DirectoryName}文件名:{fileInfo.FileName}*********{line}”)
:new BadRequestObjectResult(“请在查询字符串或请求正文中传递文件路径”);
}
私有静态文件信息GetFileInfo(字符串文件路径)
{
int index=filePath.IndexOf(“/”);
文件路径=(索引<0)
?文件路径
:filePath.Remove(索引,1);
var filePath Split=filePath.Split('/');
var fileInfo=new fileInfo();
fileInfo.Container=filePathSplit[0];
如果((filePathSplit.Length-2)>0)
{
var folderName=“”;
对于(var i=1;i0)
{
folderName+=“/”;
}
folderName+=filePathSplit[i];
}
fileInfo.DirectoryName=folderName;
}
fileInfo.FileName=filePathSplit[filePathSplit.Length-1];
返回文件信息;
}
公共类文件信息
{
公共字符串容器{get;set;}
公共字符串目录名{get;set;}
公共字符串文件名{get;set;}
}

根据您的代码和错误消息,您没有正确使用DownloadToStream方法。有关更多详细信息,请参阅


V2函数基于.Net核心环境,因此它引用了
Microsoft.WindowsAzure.Storage
程序集,具体取决于.Net标准,该标准没有同步API,这意味着我们需要*异步方法

await blockBlob2.DownloadToStreamAsync(memoryStream);

谢谢吉姆的建议。我试过了,但还是犯了这个错误。[错误CS1061:'CloudBlockBlob'不包含'DownloadToStream'的定义,并且找不到接受'CloudBlockBlob'类型的第一个参数的扩展方法'DownloadToStream'(是否缺少using指令或程序集引用?)]Jerry Liu的建议有效。但是谢谢你,吉姆给了我一个尝试的建议。
await blockBlob2.DownloadToStreamAsync(memoryStream);