当前位置: 首页 > 知识库问答 >
问题:

使用 Azure 函数迭代容器中的所有 blob

田信然
2023-03-14

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

我现在想迭代这些文件并执行它们(它们是SQL文件)。我不想触发一个基于blob创建的函数,而是在一个函数中运行它们。

在一个函数中,我如何在一个容器中迭代一个blobs列表并获取它们的内容?

谢谢

共有1个答案

齐建白
2023-03-14

如何在容器中迭代blobs列表并获取它们的内容?

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

更多细节,你可以参考下面的代码。

 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内容:

  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 并对其进行操作。 路径: {容器名称}/{目录名称}/{文件名}

  • 在我的Azure云帐户中,我可以访问多个订阅,每个订阅中有多个资源组。每个资源组又可以具有跨不同位置的多个功能。 我可以访问给定资源组的函数,如下所示:

  • 为了简单起见,假设我想使用Azure REST API从特定存储库中的所有秘密中使用秘密名称和秘密值构建一个字典。 谢谢你。

  • 在我的应用程序中,我需要数组1的给定大小k的所有子集。。n 这可以使用递归方法轻松完成,并存储沿途找到的所有子集。然而,我不希望我的应用程序使用指数级的内存量[O(n^k)]。 因此,我想编写一个实现(Java)的类来迭代所述子集。 所以我可以如下使用它: 显然,我们需要存储最后返回的当前子集,它被初始化为1... k。但是如何从这个当前子集计算下一个子集呢?

  • 本文向大家介绍详解Python中的内建函数,可迭代对象,迭代器,包括了详解Python中的内建函数,可迭代对象,迭代器的使用技巧和注意事项,需要的朋友参考一下 Python中的内建函数和可迭代对象,迭代器 求值标识 id() #标识id 返回对象的唯一标识,CPython返回内存地址 hash() #哈希, 返回对象的哈希值 len(s) -->返回一个集合类型的元素个数 range(start,

  • 合并和拆分迭代器 # itertools_chain.py from itertools import * for i in chain([1, 2, 3], ['a', 'b', 'c']): print(i, end=' ') print() # itertools_chain_from_iterable.py from itertools import * def make_