如何解决如何在不下载的情况下读取位于 Azure 容器中的压缩 txt 文件 (blob)?
我可以使用此代码读取 txt 文件,但是当我尝试读取 txt.gz 文件时,它当然不起作用。 我如何在不下载的情况下读取压缩的 blob,因为该框架可以在云上运行? 也许可以将文件解压缩到另一个容器?但我找不到解决方案。
public static string GetBlob(string containerName,string fileName)
{
string connectionString = $"yourConnectionString";
// Setup the connection to the storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Connect to the blob storage
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
// Connect to the blob container
CloudBlobContainer container = serviceClient.GetContainerReference($"{containerName}");
// Connect to the blob file
CloudBlockBlob blob = container.GetBlockBlobReference($"{fileName}");
// Get the blob file as text
string contents = blob.DownloadTextAsync().Result;
return contents;
}
解决方法
您可以使用 GZipStream 即时解压缩您的 gz 文件,您不必担心下载它并在物理位置解压。
public static string GetBlob(string containerName,string fileName)
{
string connectionString = $"connectionstring";
// Setup the connection to the storage account
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Connect to the blob storage
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
// Connect to the blob container
CloudBlobContainer container = serviceClient.GetContainerReference($"{containerName}");
// Connect to the blob file
CloudBlockBlob blob = container.GetBlockBlobReference($"{fileName}");
// Get the blob file as text
using (var gzStream = await blob.OpenReadAsync())
{
using (GZipStream decompressionStream = new GZipStream(gzStream,CompressionMode.Decompress))
{
using (StreamReader reader = new StreamReader(decompressionStream,Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
,
无需下载,因为该框架可以在云端运行
这是不可能的。如果不下载文件,则无法处理 Blob 存储上的文件。无论您的代码在哪里运行。当然,如果您的代码也在 Azure 上运行,下载时间可能会很快,但您必须先从 blob 存储下载。
对于您的 zip 文件,您要使用 DownloadToFileAsync() 或 DownloadToStreamAsync()。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。