且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何下载和读取Azure Blob容器文件

更新时间:2023-02-14 22:35:06

此处是使用C#下载和读取Azure Blob容器文件的示例.您将需要使函数async-await连接Azure Blobs容器并在本地下载blob文件,以读取这些文件并对其执行某些操作".在该示例中,我们找到大小恰好为11K bytes的小丑文件,并将小丑文件和其他文件中每个字符的ASCII值求和,并将小丑文件ASCII值与其他文件的ASCII值最后相乘

Here is the example to download and read Azure Blobs Container Files in C#. You will need to make function async-await to connect Azure Blobs Container and download the blob files locally to read and "do something" with these file. In the example, we are finding the joker file that size exactly 11K bytes and making sum of ASCII value of each character in the joker file and other files and at the last multiplying sum of ASCII value of joker file with other files ASCII value.

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);
        }
    }
}