gpt4 book ai didi

azure - 如何在 Azure 中将多个 CloudBlockBlob 对象连接成一个 CloudBlockBlob 对象?

转载 作者:行者123 更新时间:2023-12-03 02:21:55 28 4
gpt4 key购买 nike

我正在尝试将多个 CloudBlockBlob 对象连接到 Azure 函数内的单个 CloudBlockBlob 对象中。我尝试将多个对象下载到内存流中,然后将内存流上传到新对象,但该函数在操作完成之前超时。我还尝试在读取每个输入 blob 后写入新的 blob,但写入 CloudBlockBlob 会覆盖以前的输出。我知道 CloudAppendBlob,但我希望输出文件的类型为 CloudBlockBlob。

有更好的方法吗?

这是我的代码,它将多个 CloudBlockBlob 读取到内存流中,然后将该流写入新的 CloudBlockBlob。

        public async Task CatBlob(string[] srcBlobs, string destinationBlob)
{
var connectionString = Config.AzConnStr;
var container = Config.AzContainer;

CloudStorageAccount storageAccount = null;
CloudBlobContainer cloudBlobContainer = null;

if (CloudStorageAccount.TryParse(connectionString, out storageAccount))
{
try
{
CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
cloudBlobContainer = cloudBlobClient.GetContainerReference(container);

CloudBlockBlob blockBlobDest = cloudBlobContainer.GetBlockBlobReference("subfolder/test.zip");
using (MemoryStream memStream = new MemoryStream())
{
for (int i = 0; i < srcBlobs.Length; i++)
{
CloudBlockBlob cloudBlockBlobSrc = cloudBlobContainer.GetBlockBlobReference(srcBlobs[i]);
Console.WriteLine("loop {0}", i);
await cloudBlockBlobSrc.DownloadToStreamAsync(memStream);
}
memStream.Seek(0, SeekOrigin.Begin);
await blockBlobDest.UploadFromStreamAsync(memStream);
}
}
catch (Exception ex)
{
Logger.Error("Exception while concatenating files: " + ex.Message, Context);
throw;
}
}
else
{
Logger.Error("Exception connecting to cloud storage while concatenating files", Context);
throw new Exception("Could not connect to the Azure using Connection String.");
}
}

最佳答案

请参阅下面的示例代码。它利用Azure.Storage.Blobs (version 12.9.1) SDK。我没有尝试运行此代码,因此它可能会引发一些错误。

本质上,这个想法是,您单独下载每个 blob,并立即将其内容作为 block 存储在目标 blob 中(当前您正在客户端上创建一个非常大的内存流)。上传所有 block 后,您可以提交这些 block 以创建目标 blob。

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;

namespace SO68566758
{
class Program
{
private const string connectionString = "your-connection-string";
private const string container = "your-container-name";

static async Task Main(string[] args)
{
string[] srcBlobs = new[] { "blob1.txt", "blob2.txt"};//Specify source blob names.
string destinationBlob = "subfolder/test.zip";//Specify destination blob name.
await CatBlob(srcBlobs, destinationBlob);
}

/// <summary>
/// This method downloads the blobs specified in source blobs list (one at a time)
/// and uploads the contents of that blob as a block in the destination blob. Once
/// all blocks are uploaded, block list is committed to create the destination blob.
/// </summary>
/// <param name="srcBlobs"></param>
/// <param name="destinationBlob"></param>
public static async Task CatBlob(string[] srcBlobs, string destinationBlob)
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(container);
BlockBlobClient destinationBlobClient = containerClient.GetBlockBlobClient(destinationBlob);
List<string> blockIds = new List<string>();
for (var i = 0; i < srcBlobs.Length; i++)
{
BlockBlobClient sourceBlobClient = containerClient.GetBlockBlobClient(srcBlobs[i]);
//Download source blob and read its contents as stream.
BlobDownloadResult downloadResult = await sourceBlobClient.DownloadContentAsync();
using (Stream stream = downloadResult.Content.ToStream())
{
string blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(i.ToString("d6")));
stream.Position = 0;
//Upload that as a block in the destination blob.
await destinationBlobClient.StageBlockAsync(blockId, stream);
blockIds.Add(blockId);
}
}
//All blobs have been uploaded. Now its time to commit the destination blob.
await destinationBlobClient.CommitBlockListAsync(blockIds);
}
}
}

请参阅Put BlockPut Block List REST API 操作,以了解有关如何分块上传 block blob 的更多信息。

关于azure - 如何在 Azure 中将多个 CloudBlockBlob 对象连接成一个 CloudBlockBlob 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68566758/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com