C# 如何在不下载的情况下读取Azure容器中的压缩txt文件(blob)?
我可以用这段代码读取txt文件,但是当我试图读取txt.gz文件时,它当然不起作用。 我如何在不下载的情况下阅读压缩blob,因为该框架将在云上工作? 也许可以将文件解压缩到另一个容器中?但我找不到解决办法C# 如何在不下载的情况下读取Azure容器中的压缩txt文件(blob)?,c#,azure,azure-storage-blobs,unzip,azure-blob-storage,C#,Azure,Azure Storage Blobs,Unzip,Azure Blob Storage,我可以用这段代码读取txt文件,但是当我试图读取txt.gz文件时,它当然不起作用。 我如何在不下载的情况下阅读压缩blob,因为该框架将在云上工作? 也许可以将文件解压缩到另一个容器中?但我找不到解决办法 public static string GetBlob(string containerName, string fileName) { string connectionString = $"yourConnectionString"; // Set
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;
}
无需下载,因为该框架将在云上工作
这是不可能的。如果不下载blob存储上的文件,则无法使用该文件。无论代码在哪里运行。当然,如果您的代码也在Azure上运行,下载时间可能会非常快,但是您必须首先从blob存储下载
对于zip文件,您希望使用()或()
无需下载,因为该框架将在云上工作
这是不可能的。如果不下载blob存储上的文件,则无法使用该文件。无论代码在哪里运行。当然,如果您的代码也在Azure上运行,下载时间可能会非常快,但是您必须首先从blob存储下载
对于zip文件,您可以使用()或()。您可以使用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();
}
}
}
}
您可以使用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();
}
}
}
}
你知道gz和zipping不一样吗?哦,是的,谢谢,但gz也是个问题,我需要.txt文件。你知道gz和zipping不一样吗?哦,是的,谢谢,但gz也是个问题,我需要.txt文件。Sadiqs的答案很正确。我想你明天需要阅读一些C#basicsIt的作品,谢谢:)另一个问题>>它可以从测试容器中读取,它如何读取子文件夹?我尝试将test\\testFolder1\\testFolder2作为容器名称,但遇到BlobNotFound异常。容器名称为“test”,文件名为“testFolder/testFolder2/test.txt.gz”,谢谢Sadiq Khoja。它是有效的,我很感激它的答案是正确的。我想你明天需要阅读一些C#basicsIt的作品,谢谢:)另一个问题>>它可以从测试容器中读取,它如何读取子文件夹?我尝试将test\\testFolder1\\testFolder2作为容器名称,但遇到BlobNotFound异常。容器名称为“test”,文件名为“testFolder/testFolder2/test.txt.gz”,谢谢Sadiq Khoja。很有效,我很感激