skip to Main Content

From the web page I make an API request that contains 2 params, one is file itself and one is email string that user provides. My API uploads the file to Azure Blob Storage and from there I need to trigger the blob storage function that will send the letter to email provided by user. How can I pass email to that function?

Imortant:
I can’t send email from API. My task is to send it using function.

2

Answers


  1. You have a couple of options here.

    The simplest would be to store the email address as a custom metadata property on the blob. Note that as email addresses are personal information you will want to make sure your storage account is secure.

    Set in your 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;
        }
    }
    

    Get in your blob triggered Azure function. Note you’ll have to use the BlobClient type in your trigger binding to be able to use this.

    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;
        }
    }
    

    Alternatively you could look at storing the email in Azure table storage or other data store keyed by the name of your blob. Then look it up in your blob triggered Azure function.

    Login or Signup to reply.
  2. I do want to add an alternative:

    Have to api upload the json file to Azure Blob Storage and put a message on an Azure Storage Queue (or a Service Bus Queue) containing the name of the blob and the emailaddress to send it to.

    Then create a queue triggered azure function with a blob input binding that reads the message from the queue and has a reference to the blob so it can send the email. This way you do not have to modify the blob, store sensitive information in it and keep a clean seperation between the blob and the email.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search