I am facing the issue of remote server returned error 400, bad request while uploading files to Azure as block blobs. But the strange thing is sometimes the code is worked for uploading a particular file and some time it failed for the same file.
My code is like --
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();
}
Don't know why this error is throwing and looking for possible solution.
Thanks