Search code examples
azure-webjobsazure-webjobssdk

Naming Blob Dynamically for WebJob on a Schedule


I have a web job which is creating a blob based on the return value of a WebClient call. This is working fine. But as you can see from the Blob attribute (see code below), the name of the file is static. So, it is getting overwritten every time in blob storage.

Function class:

public class Functions
{
    private static int _retryCount;
    private static readonly int _retryLimit = int.Parse(ConfigurationManager.AppSettings["retryLimit"]);
    private static readonly string _ghostRestfullUri = ConfigurationManager.AppSettings["ghostRestfullUri"];

    [NoAutomaticTrigger]
    public static void LightUpSite([Blob("ghost/response.json")] out string output, TextWriter logger)
    {
        _retryCount = 0;
        output = string.Empty;

        do
        {
            try
            {
                using (var request = new WebClient())
                {
                    var response = request.DownloadString(_ghostRestfullUri);

                    _retryCount++;

                    output = response;

                    break;
                }
            }
            catch(Exception exception)
            {
                logger.WriteLine("Job failed. Retry number:{0}", _retryCount);
            }

        } while (_retryCount < _retryLimit);
    }
}

Main menu:

public class Program
{
    static void Main()
    {
        var host = new JobHost();

        host.Call(typeof(Functions).GetMethod("LightUpSite"));
    }
}

How can I use placeholders to dynamically name the incoming file?

I have already tried the following:

  1. ghost/{name}
  2. ghost/{BlobName}

Other things to note:

This job is run on a schedule, so the host does not run and block This job does not get invoked by a trigger, it just wakes up and runs; Because the source is not coming from a message queue object or a uploaded file, I can’t figure out how I am supposed to name this blob.

Perhaps somehow using the blob storage API directly?


Solution

    1. To name an output blob dynamically use IBinder as shown in this sample
    2. To name an input blob dynamically as in a call from from Host.Call just pass the name of blob as argument:

      static void Main()
      {
          var host = new JobHost();
      
          host.Call(typeof(Functions).GetMethod("LightUpSite"), new {blobArgumentName= "container/blob"});
      }