【发布时间】:2022-06-11 14:27:15
【问题描述】:
我使用 blob 存储作为输出。但我只能创建新文件,但不能追加。有机会追加吗?
【问题讨论】:
-
我觉得可能和queueTrigger有关
标签: javascript azure-functions
我使用 blob 存储作为输出。但我只能创建新文件,但不能追加。有机会追加吗?
【问题讨论】:
标签: javascript azure-functions
有机会追加吗?
是的,您可以将数据附加到 blob。我们重现了相同的场景。
使用以下示例代码将数据附加到 blob。
Azure Function 代码示例
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Collections.Generic;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
namespace AF_append_blob_file
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string con = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
string containername = Environment.GetEnvironmentVariable("ContainerName");
string localpath = "C:/Users/v-pusharma/Desktop/2question.txt";
var filename = Path.GetFileName(localpath);
BlobContainerClient blobContainerClient = new BlobContainerClient(con, containername);
AppendBlobClient appendBlobClient = blobContainerClient.GetAppendBlobClient(containername);
appendBlobClient.CreateIfNotExists();
var maxblobsize = appendBlobClient.AppendBlobMaxAppendBlockBytes;
var buffer = new byte[maxblobsize];
byte[] buffer2 = File.ReadAllBytes(localpath);
MemoryStream memoryStream = new MemoryStream(buffer2);
if (memoryStream.Length <= maxblobsize)
{
appendBlobClient.AppendBlock(memoryStream);
}
else
{
var byteleft = (memoryStream.Length - memoryStream.Position);
while (byteleft > 0)
{
if (byteleft >= maxblobsize)
{
buffer = new byte[maxblobsize];
await memoryStream.ReadAsync(buffer, 0, maxblobsize);
}
else
{
buffer = new byte[byteleft];
await memoryStream.ReadAsync
(buffer, 0, Convert.ToInt32(byteleft));
}
appendBlobClient.AppendBlock(new MemoryStream(buffer));
byteleft = (memoryStream.Length - memoryStream.Position);
}
}
return new OkObjectResult("Ok");
}
}
}
附加前的 Blob 文件,大小 (20B)
追加前的结果
第一次运行
后追加 blob 的结果,Size(40B)
第二次跑
附加输出后
请参阅 Microsoft Documentation 以将数据附加到 Azure 存储中的 blob。
【讨论】: