gpt4 book ai didi

azure - PutBlock错误: The remote server returned an error: (400) Bad Request while uploading file to Azure

转载 作者:行者123 更新时间:2023-12-02 07:41:49 25 4
gpt4 key购买 nike

我在将文件作为 block blob 上传到 Azure 时遇到远程服务器返回错误 400、错误请求的问题。但奇怪的是,有时代码可以用于上传特定文件,有时却无法上传同一文件。

我的代码就像--

        List<string> blockIdList = new List<string>();

using (var file = new FileStream(_path, FileMode.Open, FileAccess.Read))
{
int blockId = 0;

int blockSize = 4096;
// open file
while (file.Position < file.Length)
{
// calculate buffer size (blockSize in KB)
long bufferSize = blockSize * 1024 < file.Length - file.Position ? blockSize * 1024 : file.Length - file.Position;
byte[] buffer = new byte[bufferSize];
// read data to buffer
file.Read(buffer, 0, buffer.Length);
// save data to memory stream and put to storage
using (var stream = new MemoryStream(buffer))
{
// set stream position to start
stream.Position = 0;
convert block id to Base64 Encoded string
var blockIdBase64 = Convert.ToBase64String(System.BitConverter.GetBytes(blockId));

blockBlob.PutBlock(blockIdBase64, stream, null);
blockIdList.Add(blockIdBase64);
// increase block id
blockId++;
}
}

blockBlob.PutBlockList(blockIdList);

file.Close();
}

不知道为什么会抛出此错误并寻找可能的解决方案。

谢谢

最佳答案

我注意到的一件事是您使用整数值作为blockId。这可能是上传失败的原因之一,因为所有 blockId 的长度必须相同。因此,如果文件被分成 10 个 block (blockId = 0 - 9),您的上传代码将起作用。但是如果文件被分割成超过10个 block ,上传就会失败。

我的建议是用 0 填充字符串,以便所有 blockId 的长度相同。由于您可以将一个 blob 拆分为最多 50,000 个 block ,因此执行 blockId.ToString("d6") 应该可以解决问题。

您可能还会发现这篇博文很有用:http://gauravmantri.com/2013/05/18/windows-azure-blob-storage-dealing-with-the-specified-blob-or-block-content-is-invalid-error/

关于azure - PutBlock错误: The remote server returned an error: (400) Bad Request while uploading file to Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20797811/

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