使用 Azure Function 迭代容器中的所有 blob
Iterating all blobs in a container using Azure Function
我有一个现有的 Azure 函数,它可以解压缩文件并将每个文件添加为 blob。
我现在想迭代这些文件并执行它们(它们是 SQL 文件)。我不想触发基于 blob 创建的函数,而是 运行 在一个函数中通过它们。
在函数中,如何迭代容器中的 blob 列表并获取它们的内容?
谢谢
how do I iterate a list of blobs in a container and get their contents?
根据您的描述,我建议您可以使用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。
我现在想迭代这些文件并执行它们(它们是 SQL 文件)。我不想触发基于 blob 创建的函数,而是 运行 在一个函数中通过它们。
在函数中,如何迭代容器中的 blob 列表并获取它们的内容?
谢谢
how do I iterate a list of blobs in a container and get their contents?
根据您的描述,我建议您可以使用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);
}
}
}