简体   繁体   中英

Iterating all blobs in a container using Azure Function

I've got an existing Azure Function that unzips a file and adds each file as a 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?

Thanks

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM