gpt4 book ai didi

c# - 如何在不下载的情况下读取位于Azure容器中的Zipped txt文件(blob)?

转载 作者:行者123 更新时间:2023-12-03 05:30:40 25 4
gpt4 key购买 nike

我可以使用此代码读取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();
}
}
}
}

关于c# - 如何在不下载的情况下读取位于Azure容器中的Zipped txt文件(blob)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65749198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com