Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不下载的情况下读取Azure容器中的压缩txt文件(blob)?_C#_Azure_Azure Storage Blobs_Unzip_Azure Blob Storage - Fatal编程技术网

C# 如何在不下载的情况下读取Azure容器中的压缩txt文件(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

我可以用这段代码读取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;
}
无需下载,因为该框架将在云上工作

这是不可能的。如果不下载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。很有效,我很感激