我想在 Microsoft Azure 中查找存储帐户,列出特定容器(在存储帐户中)中的 blob,然后将列出的 blob 名称存储到数据库表中。任何人请建议一个 C# 代码来将 blob 名称列表存储到数据库表中。
namespace ListStorageAccntFiles
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
//Code to list the blobnames in Console
CloudStorageAccount StorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
var BlobClient = StorageAccount.CreateCloudBlobClient();
var Container = BlobClient.GetContainerReference("samples-workitems");
var list = Container.ListBlobs();
List<string> blobNames = list.OfType<CloudBlockBlob>().Select(b => b.Name).ToList();
blobNames.ForEach(Console.WriteLine);
//Code to store blobnames under the column header "name" in a database table
}
}
}
最佳答案
with the scenario!!!! program that can look at storage account and process all new & modified files and take an action if there is any new ones
我认为,我建议使用WebJobs SDK的BlobTrigger来达到你的目的。下面是我的代码示例,以便你更好地理解它:
WebJob 类
public class Functions
{
//This function will get triggered/executed when a blob is created or updated on an Azure Blob container called trigger-blob-input.
public static void TriggerAzureBlob([BlobTrigger("trigger-blob-input/{name}")] ICloudBlob blob)
{
Console.WriteLine("Blob name:" + blob.Name);
//do something with the created/updated blob
}
}
当按如下方式创建/更新 blob 时:
您可以通过 BlobTrigger 检测 blob 并获得以下输出:
更多详细信息,您可以引用以下教程:
how to create a WebJob and deploy it to Azure
how to trigger a function when a blob is created or updated
此外,WebJobs SDK 还会扫描日志文件以监视新的或更改的 blob。此过程不是读取时的,因此在创建/更新 blob 后的短时间内或较长时间内可能不会触发函数。
如果 blob 触发器的限制不满足您的应用程序,您可以在创建/更新 blob 时创建队列消息,然后使用 QueueTrigger而不是处理 Blob 的函数上的 BlobTrigger。
关于c# - 将列表 <string> 存储到 SQL 数据库表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122699/