How to change a blob whern I store in a container

Tony Johansson 40 Reputation points
2023-05-12T06:33:02.38+00:00

Hello,

I have an azure function that is using a blob trigger. A file is dropped in a container every 30 miniutes. The azure function start and process the file(Blob) and store the file in archive container. But every file that is dropped have the same filename so I must in some way append a timestamp to the filename(Blob). If the blob trigger was activated at 2023-05-12T08:18:03 and the filename is foo.xml then the renamed file should be foo2023-05-12T08:18:03 .xml. I have been searching but haven't find a way so far. I use C# as the programing language in the azure function.

Many thanks in advance.
Tony Johansson

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,321 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,415 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,449 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,306 questions
{count} votes

2 answers

Sort by: Most helpful
  1. AirGordon 7,105 Reputation points
    2023-05-12T11:44:51.1+00:00

    If you leverage imperative binding then you will be able to set the filename via the declaration of a BlobAttribute.

    There's a fantastic blog post about doing just this here; https://www.davidguida.net/how-to-dynamically-set-blob-name-in-an-azure-function/

    You can also see another example of this binding in this post `

    BlobAttribute($"audits/{DateTime.UtcNow.ToString("yyyy-MM-dd")}/{endpointName}/{id}.json")
    

    https://weblogs.asp.net/sfeldman/azure-functions-to-make-audit-queue-and-auditors-happy


  2. Ben Gimblett 3,410 Reputation points Microsoft Employee
    2023-05-12T11:45:43.9633333+00:00

    Hi Tony - thanks for the question
    It was not clear in the question whether you're using an output binding to write the blob, but if you are you can use patterns to control the output file name. For date time specifically see here https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-expressions-patterns?WT.mc_id=DOP-MVP-5003878#current-time other patterns are described on the same page