Reputation: 1943
I am making use of the following program for uploading large files to azure blob storage. When uploading a small file less than 500KB , the program works fine otherwise I am getting an error in the following line:
blob.PutBlock(blockIdBase64, stream, null);
as "An unhandled exception of type 'Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll
Additional information: The remote server returned an error: (400) Bad Request."
There is no details about the exception so I am not sure whats the problem . Is there any suggestion regarding what could be the wrong thing in the program below:
class Program
{
static void Main(string[] args)
{
string accountName = "newstg";
string accountKey = "fFB86xx5jbCj1A3dC41HtuIZwvDwLnXg==";
// list of all uploaded block ids. need for commiting them at the end
var blockIdList = new List<string>();
StorageCredentials creds = new StorageCredentials(accountName, accountKey);
CloudStorageAccount storageAccount = new CloudStorageAccount(creds, useHttps: true);
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
CloudBlobContainer sampleContainer = client.GetContainerReference("newcontainer2");
string fileName = @"C:\sample.pptx";
CloudBlockBlob blob = sampleContainer.GetBlockBlobReference("APictureFile6");
using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
int blockSize = 1;
// block counter
var blockId = 0;
// open file
while (file.Position < file.Length)
{
// calculate buffer size (blockSize in KB)
var bufferSize = blockSize * 1024 < file.Length - file.Position ? blockSize * 1024 : file.Length - file.Position;
var 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(Encoding.UTF8.GetBytes(blockId.ToString(CultureInfo.InvariantCulture)));
blob.PutBlock(blockIdBase64, stream, null);
blockIdList.Add(blockIdBase64);
// increase block id
blockId++;
}
}
file.Close();
}
blob.PutBlockList(blockIdList);
}
}
Upvotes: 1
Views: 1687
Reputation: 136196
You're getting this error because your block ids are not of same length. So for 1st 9 blocks your block id length is 1 character but as soon as you reach 10th block your block id length becomes 2. Please do something like the following:
var blockIdBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId.ToString("d6", CultureInfo.InvariantCulture)));
This way all your block ids are 6 characters long.
For more details, please read URI Parameters
section here: https://msdn.microsoft.com/en-us/library/azure/dd135726.aspx.
Upvotes: 5