问题描述
我正在分块接收文件内容.正如我所读,建议与Azure blob存储一起使用的最新nuget是 Azure.Storage.Blobs
,但是我找不到任何示例或方法以块为单位上传文件吗?支持吗?
I'm receiving file content in chunks. As I've read the latest nuget recommended to work with Azure blob storage is Azure.Storage.Blobs
, but I'm unable to find any example or method how to upload the file in chunks? Is it supported?
我收到的范围是 Range
标头.我正在使用 quickstart
I'm receiving the range as Range
header. I'm using this quickstart
推荐答案
您要使用的方法是 BlockBlobClient.StageBlock
,它会上传块数据和 提交块并创建块blob.
The methods you would want to use are BlockBlobClient.StageBlock
which uploads the chunk data and BlockBlobClient.CommitBlock
which commits the blocks and creates a block blob.
这是该功能的一个非常糟糕的实现:).它基本上是从本地计算机读取一个很大的文件,并通过将文件拆分为1MB的块来上传.
Here's a really crappy implementation of the functionality :). It basically reads a really large file from the local computer and uploads it by splitting the file in 1MB chunks.
static void UploadBlobsInChunks()
{
var containerClient = new BlobContainerClient(connectionString, "test");
containerClient.CreateIfNotExists();
var filePath = @"C:\temp\mymovie.mp4";
var blockBlobClient = containerClient.GetBlockBlobClient("mymovie.mp4");
int blockSize = 1 * 1024 * 1024;//1 MB Block
int offset = 0;
int counter = 0;
List<string> blockIds = new List<string>();
using (var fs = File.OpenRead(filePath))
{
var bytesRemaining = fs.Length;
do
{
var dataToRead = Math.Min(bytesRemaining, blockSize);
byte[] data = new byte[dataToRead];
var dataRead = fs.Read(data, offset, (int) dataToRead);
bytesRemaining -= dataRead;
if (dataRead > 0)
{
var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(counter.ToString("d6")));
blockBlobClient.StageBlock(blockId, new MemoryStream(data));
Console.WriteLine(string.Format("Block {0} uploaded successfully.", counter.ToString("d6")));
blockIds.Add(blockId);
counter++;
}
}
while (bytesRemaining > 0);
Console.WriteLine("All blocks uploaded. Now committing block list.");
var headers = new BlobHttpHeaders()
{
ContentType = "video/mp4"
};
blockBlobClient.CommitBlockList(blockIds, headers);
Console.WriteLine("Blob uploaded successfully!");
}
}
这篇关于将视频上传到Azure Azure Blob存储中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!