ccoutinho
ccoutinho

Reputation: 4556

Dynamic output blob storage container names

When configuring the output blob storage container for an Azure function, is it somehow possible to run some code in order to generate the path where the BLOB will be stored? To be more precise, I would like to use a new GUID within the path, every time this function would be triggered. Something like this (code does not work):

[FunctionName("BlobTriggered")]        
public static void BlobTriggered(
    [BlobTrigger("myContainer/{name}.{extension}")] Stream myBlob,
    [Blob("myContainer/{Guid.NewGuid()}", FileAccess.Write)] Stream outputContainer,

    string name,
    string extension,
    TraceWriter log)
{
    ...
}

In the code above, I am trying to generate the GUID by using Guid.NewGuid(), which doesn't work. Is there a similar way to achieve this?

Upvotes: 1

Views: 765

Answers (2)

nhd79
nhd79

Reputation: 11

  1. You can use the {rand-guid} binding expression. Here is the document.

    [Blob("myContainer/{rand-guid}", FileAccess.Write)] Stream outputContainer

  2. If you want further dynamic naming, use binding at runtime. Here is the document.

[FunctionName("BlobTriggered")]        
public static void BlobTriggered(
    [BlobTrigger("myContainer/{name}.{extension}")] Stream myBlob,
    string name,
    string extension,
    TraceWriter log,
    IBinder binder)
{
    var blob = binder.Bind<BlobClient>(new BlobAttribute($"myContainer/{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}", FileAccess.Write));
    blob.Upload({{your-file}});
}

Upvotes: 0

suziki
suziki

Reputation: 14088

You can set the variable in {} and set the corresponding parameter in the declaration section to get this value in the attribute. But because the parameters of the function declaration part must be fixed at compile time, I think your idea cannot be completed using binding. But you can still achieve what you want, please have a look of the below code, I am using Storage Blob SDK:

using System;
using System.IO;
using Azure.Storage.Blobs;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp53
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("samples-workitems/{name}.{extension}", Connection = "str")]Stream myBlob, 
            string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
            string connectionString = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;xxx;EndpointSuffix=core.windows.net";
            BlobServiceClient myClient = new BlobServiceClient(connectionString);
            var container = myClient.GetBlobContainerClient("samples-workitems");
            string a = Guid.NewGuid().ToString();
            var blockBlob = container.GetBlobClient(a);
            blockBlob.Upload(myBlob);
        }
    }
}

Upvotes: 1

Related Questions