c# - 无法从 Azure 函数中的 Blob 存储读取文件
问题描述
我正在尝试使用 Azure Function App 从 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; }
}
解决方案
V2 函数基于 .Net Core env,因此它引用了Microsoft.WindowsAzure.Storage
依赖于 .Net Standard 的程序集,它没有同步 API,这意味着我们需要 *Async 方法。
await blockBlob2.DownloadToStreamAsync(memoryStream);
推荐阅读
- java - Groovy 脚本性能包含 vs 匹配
- node.js - 在树中找到最小的节点
- firebase - 在 Flutter 中将数据保存到 Firebase 实时数据库
- python - Tensorflow 中的“阵列”检测
- kubernetes - 如何确定 Kind Cluster 中的节点(cpu、内存)?
- ios - Xcode 卡在从设备 tvOS 复制缓存文件
- python - 如何为数据框制作滚动渐变列
- flutter - Flutter:与 Provider 的 UI 反应
- oracle - Oracle XE 和 DBeaver:ORA-12514,TNS:listener 目前不知道连接描述符中请求的服务
- c - 找不到命令分配数组脚本bash