c# - 如何将附加参数传递给 Blob 触发函数

标签 c# azure azure-functions azure-blob-storage

我从网页发出一个包含 2 个参数的 API 请求,一个是文件本身,一个是用户提供的电子邮件字符串。我的 API 将文件上传到 Azure Blob 存储,然后我需要触发 Blob 存储功能,将信件发送到用户提供的电子邮件。我如何将电子邮件传递给该函数?

重要: 我无法从 API 发送电子邮件。我的任务是使用函数发送它。

最佳答案

这里有几个选项。

最简单的方法是将电子邮件地址存储为 Blob 上的自定义元数据属性。请注意,由于电子邮件地址是个人信息,因此您需要确保存储帐户的安全。

在 API 中设置

public static async Task AddBlobMetadataAsync(BlobClient blob, string email)
{
    try
    {
        IDictionary<string, string> metadata =
           new Dictionary<string, string>();

        // Add metadata to the dictionary by using key/value syntax
        metadata["email"] = email;

        // Set the blob's metadata.
        await blob.SetMetadataAsync(metadata);
    }
    catch (RequestFailedException e)
    {
        // Error handling here
        throw;
    }
}

获取 Blob 触发的 Azure 函数。请注意,您必须在触发器绑定(bind)中使用 BlobClient 类型才能使用此功能。

private static async Task<string> GetBlobMetadataEmailAsync(BlobClient blob)
{
    try
    {
        // Get the blob properties
        BlobProperties properties = await blob.GetPropertiesAsync();

        
     if(properties.Metadata.ContainsKey("email")
    {
        return properties.Metadata["email"];
    }
    else
    {
        // Handle the case that it potentially doesn't 
    }

    }
    catch (RequestFailedException e)
    {
        // Exception handling here
        throw;
    }
}

或者,您可以考虑将电子邮件存储在 Azure 表存储或其他以 Blob 名称为键的数据存储中。然后在 blob 触发的 Azure 函数中查找它。

关于c# - 如何将附加参数传递给 Blob 触发函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75776511/

相关文章:

wcf - 将 ClaimCollection 从 Silverlight 发送到 WCF

c# - Azure Service Fabric FabricObjectClosedException

azure - 安装 Azure SDK 1.8 时,云模板不会安装在 VS2012 中

azure - 运行 azure 函数时 Intellij 中出现错误 "Failed to get version of function(null)"

c# - 在逻辑应用程序中运行时Azure函数404错误

c# - 如何去除MDI父窗体的灰色背景?

c# - 如何在WPF中选择日期和时间

azure - 如何从Http触发函数发送消息到消息总线?

c# Interop.Word 在不保存到磁盘的情况下连接两个 WordDocument

c# - 如何根据浏览器在同一页面上嵌入 32 位或 64 位 ActiveX 控件?