使用 BlobOutputAttribute 时动态设置名称和/或容器名称

问题描述 投票:0回答:1

我正在开发一个由服务总线队列触发的天蓝色函数,并最终使用“BlobOutputAttribute”绑定将内容发布到 Blob 存储容器:

    [Function(nameof(Function))]
    [BlobOutput("container/test.txt", Connection = "AzureWebJobsStorage")]
    public async Task<string> Run(
        [ServiceBusTrigger("%ServicebusQueueName%", Connection = "ServicebusConnectionString")] 
        ServiceBusReceivedMessage message, 
        ServiceBusMessageActions messageActions)
    {
        try {
            logger.LogInformation("Message Body: {body}", message.Body);
    
            // Complete the message
            await messageActions.CompleteMessageAsync(message);
    
            // Send content to blob storage
            return message.Body.ToString();
        }
        catch(Exception ex) {
            logger.LogError(ex, ex.Message);
            throw;
        }
    } 

有没有办法动态设置容器的名称和文件的名称? 我研究过“绑定表达式模式”,但从我在文档和 Youtube 上的一些剪辑中看到的内容来看,我只遇到过 {rand-guid} 示例,每个人很快都只说“它自动生成一个指导”。不完全是我想要的。

binding azure-functions azure-blob-storage azure-functions-isolated binding-expressions
1个回答
0
投票

您可以使用以下代码动态设置 blob 名称并将 blob 上传到存储容器。

您可以使用时间戳或消息 ID 生成 blob 名称。

代码片段:

[Function(nameof(Function1))]
public async Task Run(
    [ServiceBusTrigger("sbqueue", Connection = "demo")]
    ServiceBusReceivedMessage message,
    ServiceBusMessageActions messageActions)
{
    _logger.LogInformation("Message ID: {id}", message.MessageId);
    _logger.LogInformation("Message Body: {body}", message.Body);
    _logger.LogInformation("Message Content-Type: {contentType}", message.ContentType);

    await messageActions.CompleteMessageAsync(message);

    var containerName = "container";
    var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
    var blobServiceClient = new BlobServiceClient(connectionString);
    var containerClient = blobServiceClient.GetBlobContainerClient(containerName);

    // Generate a dynamic blob name based on message ID or timestamp
    var fileName = $"{DateTime.UtcNow}.txt";
 // var fileName = $"{Message.MessageId}.txt";
   
    var blobClient = containerClient.GetBlobClient(fileName);
    using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(message.Body.ToString())))
    {
        await blobClient.UploadAsync(stream, overwrite: true); 
    }

    _logger.LogInformation("Blob uploaded: {container}/{fileName}", containerName, fileName);
}

使用日期时间作为 blobname:

控制台输出:

Functions:

Function1: serviceBusTrigger

For detailed output, run func with --verbose flag.  
[2024-11-27T09:05:50.813Z] Host lock lease acquired by instance ID '0000000000000000000000000C237F69'.  
[2024-11-27T09:06:33.092Z] Executing 'Functions.Function1' (Reason='(null)', Id=25e6a342-9ee8-427a-afc5-fd1a801d1c2f)  
[2024-11-27T09:06:33.098Z] Trigger Details: MessageId: 9c459abdecde448aa2cd5fb00d2c7ec2, SequenceNumber: 10, DeliveryCount: 1, EnqueuedTimeUtc: 2024-11-27T09:06:29.9050000+00:00, LockedUntilUtc: 2024-11-27T09:07:29.9520000+00:00, SessionId: (null)  
[2024-11-27T09:06:33.627Z] Message ID: 9c459abdecde448aa2cd5fb00d2c7ec2  
[2024-11-27T09:06:33.630Z] Message Body: Hello World  
[2024-11-27T09:06:33.635Z] Message Content-Type: (null)  
[2024-11-27T09:06:33.676Z] Start processing HTTP request POST [http://127.0.0.1:59633/Settlement/Complete](http://127.0.0.1:59633/Settlement/Complete "http://127.0.0.1:59633/settlement/complete")  
[2024-11-27T09:06:33.686Z] Sending HTTP request POST [http://127.0.0.1:59633/Settlement/Complete](http://127.0.0.1:59633/Settlement/Complete "http://127.0.0.1:59633/settlement/complete")  
[2024-11-27T09:06:34.216Z] Received HTTP response headers after 513.4688ms - 200  
[2024-11-27T09:06:34.224Z] End processing HTTP request after 556.9005ms - 200  
[2024-11-27T09:06:35.555Z] Blob uploaded: container1/11/27/2024 9:06:34 AM.txt  
[2024-11-27T09:06:35.627Z] Executed 'Functions.Function1' (Succeeded, Id=25e6a342-9ee8-427a-afc5-fd1a801d1c2f, Duration=2587ms)

传送门:

在存储容器中使用时间戳作为 Blob 名称生成 Blob。

enter image description here

使用 MessageID 作为 blobname:

控制台输出:

[2024-11-27T09:28:31.101Z] Executing 'Functions.Function1' (Reason='(null)', Id=e45f0dfd-4f87-4b38-b6f4-0c277c5eb842)  
[2024-11-27T09:28:31.107Z] Trigger Details: MessageId: f1c475407b2646a99764b6fcae4eb6f6, SequenceNumber: 13, DeliveryCount: 1, EnqueuedTimeUtc: 2024-11-27T09:28:22.2990000+00:00, LockedUntilUtc: 2024-11-27T09:29:28.0030000+00:00, SessionId: (null)  
[2024-11-27T09:28:31.719Z] Message ID: f1c475407b2646a99764b6fcae4eb6f6  
[2024-11-27T09:28:31.725Z] Message Body: Hello World  
[2024-11-27T09:28:31.736Z] Message Content-Type: (null)  
[2024-11-27T09:28:31.777Z] Start processing HTTP request POST [http://127.0.0.1:59848/Settlement/Complete](http://127.0.0.1:59848/Settlement/Complete "http://127.0.0.1:59848/settlement/complete")  
[2024-11-27T09:28:31.790Z] Sending HTTP request POST [http://127.0.0.1:59848/Settlement/Complete](http://127.0.0.1:59848/Settlement/Complete "http://127.0.0.1:59848/settlement/complete")  
[2024-11-27T09:28:32.261Z] Received HTTP response headers after 445.59ms - 200  
[2024-11-27T09:28:32.269Z] End processing HTTP request after 503.7729ms - 200  
[2024-11-27T09:28:33.490Z] Host lock lease acquired by instance ID '0000000000000000000000000C237F69'.  
[2024-11-27T09:28:33.631Z] Blob uploaded: container1/f1c475407b2646a99764b6fcae4eb6f6.txt  
[2024-11-27T09:28:33.692Z] Executed 'Functions.Function1' (Succeeded, Id=e45f0dfd-4f87-4b38-b6f4-0c277c5eb842, Duration=2642ms)

传送门:

在存储容器中使用时间戳作为 Blob 名称生成 Blob。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.