【问题标题】:How to download and read Azure blob container files [closed]如何下载和读取 Azure blob 容器文件 [关闭]
【发布时间】:2020-09-08 21:49:33
【问题描述】:

我需要从 Azure blob 容器中下载所有文件,然后逐个读取文件以找到所有 ASCII 字符值的总和。

【问题讨论】:

  • 我认为您想编辑您的问题但发布了一个答案,您能纠正一下吗?
  • 我不确定您到底在寻找什么。您遇到问题了吗?请编辑您的问题并包含这些详细信息。
  • @devcrp,非常感谢您让我知道。我是这里的新手,并为某人的问题添加了这个答案。现在好看吗?
  • @Sajan 问题是,在阅读了您的问题后,我们实际上不知道问题出在哪里。您描述了您的情况,但没有提出任何要求。

标签: c# async-await azure-devops azure-blob-storage azure-container-service


【解决方案1】:

这是在 C# 中下载和读取 Azure Blobs 容器文件的示例。您将需要创建函数async-await 来连接 Azure Blob 容器并在本地下载 Blob 文件以读取这些文件并“做某事”。在示例中,我们找到大小正好为 11K bytes 的小丑文件,并将小丑文件和其他文件中每个字符的 ASCII 值相加,最后将小丑文件的 ASCII 值与其他文件的 ASCII 值相乘.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;

namespace myApp
{
    class Program
    {
        static async Task Main(string[] args)
        {   /**********************************************************************************************
            * Sum the ASCII value - find the joker (228326687915660 = 329081602*693830)
            ***********************************************************************************************/
            Uri blobContainerUri = new Uri("blob-container-uri-find-the-joker");
            BlobContainerClient blobContainerClient = new BlobContainerClient(blobContainerUri, null);
            string localPath = "./data/";
            ulong sumASCIIValue = 0ul;
            ulong sumJokerASCIIValue = 0ul;

            await foreach (var blob in blobContainerClient.GetBlobsAsync())
            {
                string fileName = blob.Name;
                string localFilePath = Path.Combine(localPath, fileName);

                using (var file = File.Open(localFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    var blobClient = blobContainerClient.GetBlobClient(blob.Name);
                    await blobClient.DownloadToAsync(file);

                    if (file.CanRead)
                    {
                        file.Position = 0;

                        byte[] readBytes = new byte[file.Length];

                        // Detect joker file
                        if (file.Length >= 11000)
                        {
                            while (file.Read(readBytes, 0, readBytes.Length) > 0)
                            {
                                string asciiString = System.Text.Encoding.ASCII.GetString(readBytes);
                                foreach (Char chr in asciiString)
                                {
                                    sumJokerASCIIValue += (ulong)chr;
                                }
                            }
                        }
                        else
                        {
                            while (file.Read(readBytes, 0, readBytes.Length) > 0)
                            {
                                string asciiString = System.Text.Encoding.ASCII.GetString(readBytes);
                                foreach (Char chr in asciiString)
                                {
                                    sumASCIIValue += (ulong)chr;
                                }
                            }
                        }
                    }
                }

                Console.WriteLine("File Read: {0} sumASCIIValue: {1}, sumJokerASCIIValue: {2}", fileName, sumASCIIValue, sumJokerASCIIValue);
            }

            Console.WriteLine("ASCII value: {0}", sumASCIIValue * sumJokerASCIIValue);
        }
    }
}

【讨论】:

  • 从 C# 8.0 开始可以使用 await foreach
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2021-04-21
  • 2018-04-21
  • 1970-01-01
  • 2021-10-13
  • 2019-09-25
相关资源
最近更新 更多