我搜索了又搜索,但没有找到任何例子。
我在 C# .NET Core 中使用 Azure.Storage.Blobs nuget 包。
这是我当前代码不起作用的示例。
我收到状态:413(请求正文太大,超出了最大允许限制。)
搜索似乎表明有 4mb 限制或 100mb 限制,尚不清楚,但我认为追加 Blob 上有 4mb 限制,块 Blob 上有 100mb 限制。
var appendBlobClient = containerClient.GetAppendBlobClient(string.Format("{0}/{1}", tenantName, Path.GetFileName(filePath)));
using FileStream uploadFileStream = File.OpenRead(filePath);
appendBlobClient.CreateIfNotExists();
appendBlobClient.AppendBlock(uploadFileStream);
uploadFileStream.Close();
由于 4mb 的限制,这不起作用,因此我需要附加 4mb 的文件块,但我还没有找到执行此操作的最佳方法的示例。
所以我想找出的是上传大文件的最佳方法,它似乎必须分块完成,对于附加 blob 可能是 4mb,对于块 blob 可能是 100mb,但文档不清楚,也没有示例。
我要感谢@silent 的回复,因为他提供了足够的信息来确定我需要什么。有时候,只要有人谈论事情就可以帮助我解决问题。
我在 BlockBlobClient.Upload 方法中发现的内容是为您分块文件流。我相信这是我研究中的 100mb 块。似乎有 100mb 块和 50,000 个块的限制
对于 AppendBlockClient.AppendBlock 它不会为您分块流。它有 4mb 块和 50,000 个块的限制。
这是我的代码的一部分,它允许我上传 6GB 文件作为块 Blob 和 200MB 文件作为附加 Blob。
BlobServiceClient blobServiceClient = new BlobServiceClient(azureStorageAccountConnectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(azureStorageAccountContainerName);
containerClient.CreateIfNotExists();
if (appendData)
{
var appendBlobClient = containerClient.GetAppendBlobClient(string.Format("{0}/{1}", tenantName, Path.GetFileName(filePath)));
appendBlobClient.CreateIfNotExists();
var appendBlobMaxAppendBlockBytes = appendBlobClient.AppendBlobMaxAppendBlockBytes;
using (var file = File.OpenRead(filePath))
{
int bytesRead;
var buffer = new byte[appendBlobMaxAppendBlockBytes];
while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
{
//Stream stream = new MemoryStream(buffer);
var newArray = new Span<byte>(buffer, 0, bytesRead).ToArray();
Stream stream = new MemoryStream(newArray);
stream.Position = 0;
appendBlobClient.AppendBlock(stream);
}
}
}
else
{
var blockBlobClient = containerClient.GetBlockBlobClient(string.Format("{0}/{1}", tenantName, Path.GetFileName(filePath)));
using FileStream uploadFileStream = File.OpenRead(filePath);
blockBlobClient.DeleteIfExists();
blockBlobClient.Upload(uploadFileStream);
uploadFileStream.Close();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)