Search code examples
azureazure-blob-storageazure-functionsazure-storage-queues

store in blob storage from queue trigger


This question is edited as I did not have a good idea about how binding works in azure functions and the my question was poorly formed.

basically, I wanted to know how to write store a blob to a blog storage which has to be triggered by a queue msg (meaning the blob should be created in an azure queue trigger and then stored to a blob storage).

Tom Sun has given the answer which I was expecting


Solution

  • If you want to store the queue messages to an azure blob via queue trigger, you could use the cloudAppendblob. The following the demo code

    [FunctionName("TestQueueTrigger")]
    public static void Run([QueueTrigger("queueName", Connection = "AzureWebJobsStorage")]string myQueueItem,TraceWriter log)
    {
    
       log.Info($"C# Queue trigger function processed: {myQueueItem}");
       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("AzureWebJobsStorage"));
       CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
       CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
    // Create the container if it doesn't already exist.
       container.CreateIfNotExists();
    
       var appendBlob = container.GetAppendBlobReference("blobName.txt");
    
       if (!appendBlob.Exists())
       {
            appendBlob.CreateOrReplace();
            appendBlob.AppendText(myQueueItem);
       }
       else
       {
          appendBlob.AppendText($",{myQueueItem}");
       }
    
    
     }