I recently needed to update a Windows Azure customer project to use the current version of Azure Storage Client library. Luckily NuGet automates most of the package management activities (adding references, dependencies etc). Apparently there have been some dramatic changes in the structure of the library itself, since I had to do quite a lot of work to bring the code back up to working level.
Some issues I bumped into:
Microsoft.WindowsAzure.StorageClient namespace has been renamed to Microsoft.WindowsAzure.Storage
CloudStorageAccount.SetConfigurationSettingPublisher() is no longer supported, use CloudConfigurationManager.GetSetting() instead. Typically like this:
Account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(configurationSettingName));
StorageClientException class renamed to StorageException, notably the following code:
catch (StorageClientException ex)
{
if ((int)ex.StatusCode == 404)
{
return false;
}throw;
}
becomes:
catch (StorageException ex)
{
if ((int)ex.RequestInformation.HttpStatusCode == 404)
{
return false;
}throw;
}
Uploading/downloading blobs only support streams instead of the earlier options that included byte array, string etc.
So instead of this code:
public void SaveImage(string containerName, string blobName, string contentType, byte[] data)
{
CloudBlobContainer container = BlobClient.GetContainerReference(containerName);
CloudBlob blob = container.GetBlobReference(blobName);
blob.Properties.ContentType = contentType;
blob.UploadByteArray(data);
}
you need to do something like this:
public void SaveImage(string containerName, string blobName, string contentType, byte[] data)
{
CloudBlobContainer container = BlobClient.GetContainerReference(containerName);
ICloudBlob blob = container.GetBlockBlobReference(blobName);
blob.Properties.ContentType = contentType;
var blockBlob = blob as CloudBlockBlob;
using (var stream = new MemoryStream(data, writable: false))
{
blockBlob.UploadFromStream(stream);
}
}
Note the use of GetBlockBlobReference() method instead of GetBlobReferenceFromServer() in the new code. The latter actually hits the server (thus resulting in performance hit), but what’s more crucial is that it actually fails if the blob does not exist. So if you need to create a new blob, it cannot be used at all, so most likely you will end up using GetBlockBlobReference() instead.
Retry policy structure has changed. Instead of this:
BlobClient.RetryPolicy = RetryPolicies.Retry(4, TimeSpan.Zero);
you need to do something like this:
using Microsoft.WindowsAzure.Storage.RetryPolicies;
BlobClient.RetryPolicy = new LinearRetry(TimeSpan.Zero, 4);
Links