I have a scenario to upload databases backups to the Azure Blob storage via Window Service. It is working for the bak files size range between 300-500 MB but if the size exceeds 700 MB to 1 GB or more. It took more than an hour and then throw an exception.
Please check the code below let me know what I am doing wrong and what is the efficient method to upload large size files to blob storage. I have tried these two methods.
public static void UploadFile(AzureOperationHelper azureOperationHelper)
{
CloudBlobContainer blobContainer = CreateCloudBlobContainer(tenantId, applicationId,
clientSecret, azureOperationHelper.storageAccountName, azureOperationHelper.containerName,
azureOperationHelper.storageEndPoint);
blobContainer.CreateIfNotExists();
var writeOptions = new BlobRequestOptions()
{
SingleBlobUploadThresholdInBytes = 50 * 1024 * 1024,//maximum for 64MB,32MB by default
ParallelOperationThreadCount = 12,
};
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(azureOperationHelper.blobName);
//blob.UploadFromFile(azureOperationHelper.srcPath);
blob.UploadFromFile(azureOperationHelper.srcPath, options: writeOptions);
}
public static void UploadFileStream(AzureOperationHelper azureOperationHelper)
{
CloudBlobContainer blobContainer = CreateCloudBlobContainer(tenantId, applicationId,
clientSecret, azureOperationHelper.storageAccountName, azureOperationHelper.containerName,
azureOperationHelper.storageEndPoint);
blobContainer.CreateIfNotExists();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(azureOperationHelper.blobName);
//byte[] contents = File.ReadAllBytes(azureOperationHelper.srcPath);
//var writeOptions = new BlobRequestOptions()
//{
// SingleBlobUploadThresholdInBytes = 50 * 1024 * 1024,//maximum for 64MB,32MB by default
// ParallelOperationThreadCount = 12,
//};
//blob.UploadFromByteArray(contents, 0, contents.Length, AccessCondition.GenerateIfNotExistsCondition(), options: writeOptions);
blob.StreamWriteSizeInBytes = 100 * 1024 * 1024; //100 MB
blob.UploadFromFile(string.Format(azureOperationHelper.srcPath));
//using (var fs = new FileStream(azureOperationHelper.srcPath, FileMode.Open))
//{
// blob.UploadFromStream(fs);
//}
}
Below are the exceptions I got.
Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden. at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[T](HttpStatusCode expectedStatusCode, HttpStatusCode actualStatusCode, T retVal, StorageCommandBase`1 cmd, Exception ex)
Microsoft.WindowsAzure.Storage.StorageException: The client could not finish the operation within specified timeout. ---> System.TimeoutException: The client could not finish the operation within specified timeout.