我从网页发出一个包含 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/