【发布时间】:2021-04-02 18:07:56
【问题描述】:
下面是从我的 blob 存储中读取 blob 然后将内容复制到表格存储中的代码。现在一切正常。但我知道如果我的文件太大,那么它会导致读取和复制失败。我想知道我们如何理想地处理这个问题,是我们暂时写入文件而不是将其存储在内存中吗?如果是,有人可以给我举个例子或告诉我如何在下面的现有代码中做到这一点>
public async Task<Stream> ReadStream(string containerName, string digestFileName, string fileName, string connectionString)
{
string data = string.Empty;
string fileExtension = Path.GetExtension(fileName);
var contents = await DownloadBlob(containerName, digestFileName, connectionString);
return contents;
}
public async Task<Stream> DownloadBlob(string containerName, string fileName, string connectionString)
{
Microsoft.Azure.Storage.CloudStorageAccount storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = serviceClient.GetContainerReference(containerName);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
if (!blob.Exists())
{
throw new Exception($"Unable to upload data in table store for document");
}
return await blob.OpenReadAsync();
}
private IEnumerable<Dictionary<string, EntityProperty>> ReadCSV(Stream source, IEnumerable<TableField> cols)
{
using (TextReader reader = new StreamReader(source, Encoding.UTF8))
{
var cache = new TypeConverterCache();
cache.AddConverter<float>(new CSVSingleConverter());
cache.AddConverter<double>(new CSVDoubleConverter());
var csv = new CsvReader(reader,
new CsvHelper.Configuration.CsvConfiguration(global::System.Globalization.CultureInfo.InvariantCulture)
{
Delimiter = ";",
HasHeaderRecord = true,
CultureInfo = global::System.Globalization.CultureInfo.InvariantCulture,
TypeConverterCache = cache
});
csv.Read();
csv.ReadHeader();
var map = (
from col in cols
from src in col.Sources()
let index = csv.GetFieldIndex(src, isTryGet: true)
where index != -1
select new { col.Name, Index = index, Type = col.DataType }).ToList();
while (csv.Read())
{
yield return map.ToDictionary(
col => col.Name,
col => EntityProperty.CreateEntityPropertyFromObject(csv.GetField(col.Type, col.Index)));
}
}
}
【问题讨论】:
-
“复制表格存储中的内容”的位在哪里?另外,想想流——它们就像管道,你有一些你想读的东西和你想写的东西。现在你使用的方法是给块块一个内存流并写入它,然后你(可能)重置它并将它提供给 tableblob 并让它从中读取.. 但是为什么不只是询问块块它的流并将该流提供给表 blob 并从中读取?这几乎就是流的全部概念。它们只是数据流。你还在想这一切……
-
... 就“我必须读取所有数据,我必须把它放在某个地方,我必须写它...”而言 - 而是考虑如何通过给“想要读取的东西”直接连接到“想要提供要读取的数据的东西”(或者同样给“想要写入的东西”直接连接到“想要写入的东西”无需站在中间进行临时存储(内存或磁盘)的读/写操作
-
那是我正在寻找的东西,暂时阅读和编写它是我的想法..但你明白这里在说什么问题,你有任何参考的例子吗?我编辑了读取从 ReadStream() 传递的数据的代码
-
现在更困惑了。以为您在谈论 azure table storage.. 您似乎在谈论下载 blockblob 并将其转换为 CSV,但您发布了两种读取方法;我会期待读和写。概念保持不变;如果您有“提供可以读取的流的东西”(=“可以提供它将写入的流的东西”)并且您有“提供可以写入的流的东西”(=“您可以可以提供它将读取的流”)然后您可以将它们配对并让它们直接进行读/写而无需临时存储
-
好的,我在这里做的是从 azure blob 存储中读取 blob,这些 blob 基本上是 csv 文件,我正在将内容复制到 Azure 表格存储中。我没有提供将数据插入表格存储的方法,但这基本上是获取所有 ReadCsv 数据并将它们批量插入表格中。我没有找到任何为 azure blob 提供流的东西。任何想法是否支持 blob
标签: c# azure-blob-storage memorystream