1

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?

1
  • Since the parameters of the declaration part of the function must be fixed at compile time, I think your idea cannot be completed using binding. But you can use SDK. I have post an answer, please have a look.:) Commented May 26, 2020 at 1:43

2 Answers 2

1

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.