Skip to content

Instantly share code, notes, and snippets.

@codingoutloud
Created July 16, 2012 01:36
Show Gist options
  • Save codingoutloud/3119713 to your computer and use it in GitHub Desktop.
Save codingoutloud/3119713 to your computer and use it in GitHub Desktop.
Patiently create blob container; useful when container of same name was recently deleted (and full deletion may take a little time)
using Microsoft.WindowsAzure.StorageClient;
/// <summary>
/// Ensure that blobContainer exists before this method returns.
/// There is no timeout or maximum time for which this method can run.
/// </summary>
/// <param name="blobContainer">The (possibly not-yet-created) blob container.</param>
/// <param name="options">Required options for blob creation.</param>
/// <param name="retryDelay">Optional. Control how long to sleep between creation attempts.</param>
/// <returns>If success, just returns. Otherwise, exception is thrown.</returns>
public static void PatientlyCreateBlobContainer(
CloudBlobContainer blobContainer
, BlobRequestOptions options
, TimeSpan retryDelay = TimeSpan.FromMilliseconds(200))
{
bool itExists = false;
while (!itExists)
{
try
{
// blobContainer has a default retry policy
itExists = blobContainer.CreateIfNotExist(options);
return;
}
catch (StorageClientException ex)
{
if (ex.ErrorCode == StorageErrorCode.ResourceAlreadyExists
&& ex.StatusCode == System.Net.HttpStatusCode.Conflict)
{
// eat ex
System.Threading.Thread.Sleep(retryDelay);
}
else
{
throw;
}
}
}
throw new Exception(String.Format("Could not create container {0}."
, blobContainer.Name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment