使用Azure函数迭代容器中的所有Blob [英] Iterating all blobs in a container using Azure Function

查看:145
本文介绍了使用Azure函数迭代容器中的所有Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的Azure函数,该函数解压缩文件并将每个文件添加为blob.

I've got an existing Azure Function that unzips a file and adds each file as a blob.

我现在想遍历这些文件并执行它们(它们是SQL文件).我不想触发基于Blob创建的功能,而是希望在一个功能中全部运行它们.

I now want to iterate these files and execute them (they are SQL files). I don't want to trigger a Function based on blob creation but rather run through them all in a single Function.

在函数中,如何迭代容器中的斑点列表并获取其内容?

In a Function, how do I iterate a list of blobs in a container and get their contents?

谢谢

推荐答案

如何迭代容器中的斑点列表并获取其内容?

how do I iterate a list of blobs in a container and get their contents?

根据您的描述,我建议您可以使用CloudBlobContainer.ListBlobs方法列出容器中的Blob.然后,您可以使用CloudBlockBlob.DownloadToStream方法将blob下载到函数的内存流中,以获取存储blob文件的内容.

According to your description, I suggest you could use CloudBlobContainer.ListBlobs method to list the blob in the container. Then you could use CloudBlockBlob.DownloadToStream method to download the blob to the function's memory stream to get the content of your storage blob file.

更多详细信息,您可以参考以下代码.

More details, you could refer to below codes.

 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
   "connectionstring");
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Retrieve reference to a previously created container.
            CloudBlobContainer container = blobClient.GetContainerReference("contiainername");

            // Loop over items within the container and output the content, length and URI.
            foreach (IListBlobItem item in container.ListBlobs(null, false))
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;
                    string text;
                    using (var memoryStream = new MemoryStream())
                    {
                        blob.DownloadToStream(memoryStream);

                        //we get the content from the blob
                        //sine in my blob this is txt file,
                        text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
                    }
                   Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
                   Console.WriteLine(text);
                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    CloudPageBlob pageBlob = (CloudPageBlob)item;

                    Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);

                }
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    CloudBlobDirectory directory = (CloudBlobDirectory)item;
                    Getblobcontent(directory);
                    Console.WriteLine("Directory: {0}", directory.Uri);
                }
            }

在azure存储blob目录中获取blob内容:

Get the blob content in the azure storage blob directory:

  private static void Getblobcontent(CloudBlobDirectory container)
        {
            foreach (IListBlobItem item in container.ListBlobs())
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob blob = (CloudBlockBlob)item;
                    //int this method you could get the blob content in the directory

                    string text;
                    using (var memoryStream = new MemoryStream())
                    {
                        blob.DownloadToStream(memoryStream);

                        //we get the content from the blob
                        //sine in my blob this is txt file,
                        text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
                    }
                    Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);

                    Console.WriteLine(text);

                    Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);

                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    CloudPageBlob pageBlob = (CloudPageBlob)item;
                    //int this method you could get the blob content

                    Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);

                }
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    CloudBlobDirectory directory = (CloudBlobDirectory)item;
                    Getblobcontent(directory);

                    Console.WriteLine("Directory: {0}", directory.Uri);
                }
            }
        }

这篇关于使用Azure函数迭代容器中的所有Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆