在 blob 中上传文件然后在 Azure 中插入 blob 名称时触发单个 Azure 函数 SQL

Single Azure Function to Trigger when upload a file in blob then insert the blob name in Azure SQL

我是 Azure Function 的新手,我刚刚完成了 TimerTrigger for Sql Connection and BlobTrigger for Azure Blob。我厌倦了演示,对我来说效果很好。 接下来我尝试结合这两者。

当特定 Blob Container.I 中的文件 uploaded/Added 应该在我的 Azure SQL 数据库 table 中写入 blob 名称时 table。

我如何在单个 Azure 函数中实现这一点?

我还怀疑如果我们为 blob 触发器创建一个 azure 函数,那么这个函数将始终在后台 运行ning?我的意思是它会消耗背景 运行ning 成本 ?

我认为 blob 触发器的 azure 函数只会消耗成本 在 运行 期间。不是吗?

有人可以帮我解决这个问题

当然,您应该为您的方案使用 Blob 触发器。

如果您使用消费计划,您只会在每次事件执行时更改。不收取背景费用。

How could i achieve this in a Single Azure function ?

您可以使用 blob 触发器来实现它。您将从函数参数 [name] 中获取 blob 名称。然后您可以将该值保存到您的 Azure SQL 数据库中。以下示例代码供您参考。

public static void Run(Stream myBlob, string name, TraceWriter log)
{
    var str = "connection string of your sql server";
     using (SqlConnection conn = new SqlConnection(str))
     {
        conn.Open();
        var text = "insert into mytable(id, blobname) values(@id, @blobname)";

        using (SqlCommand cmd = new SqlCommand(text, conn))
        {
            cmd.Parameters.AddWithValue("id", 1);
            cmd.Parameters.AddWithValue("blobname", name);
            // Execute the command and log the # rows affected.
            var rows = cmd.ExecuteNonQuery();
            log.Info($"{rows} rows were updated");
        }
     }
}

I'm thinking that azure function for a blob trigger will consume the cost only during it's run. Isn't it?

创建 Azure 函数时需要选择托管计划。

如果您选择应用服务计划,您将需要为应用服务计划付费,这取决于您选择的层级。如果您选择消费计划,您的功能将根据两件事计费。资源消耗和执行。

资源消耗的计算方法是将以千兆字节为单位的平均内存大小乘以执行该函数所需的时间(以秒为单位)。您需要为函数消耗的 CPU 和内存付费。执行是指由您的函数处理的请求计数。请注意,消费计划定价还包括每月免费授予 100 万个请求和每月 400,000 GB-s 的资源消耗。

We can also call the Sp (like Exec spname) in the place of Insert Command?Right ?

是的,我们可以通过将 CommandType 设置为 StoredProcedure 来调用 sp。以下代码供您参考。

using (SqlCommand cmd = new SqlCommand("StoredProcedure Name", conn))
{
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
}