// The mutex must already exists public Mutex(CloudBlobContainer container, string mutexName, Exception e) { blob = container.GetBlobReference(mutexName); byte[] b1 = { 1 }; BlobRequestOptions requestOpt = new BlobRequestOptions(); bool keepGoing = true; string oldEtag = ""; int lastChange = 0; do { byte[] b; string eTag; try { blob.FetchAttributes(); eTag = blob.Attributes.Properties.ETag; if (eTag != oldEtag) { lastChange = Environment.TickCount; oldEtag = eTag; } b = blob.DownloadByteArray(); } catch (Exception) { throw e; } requestOpt.AccessCondition = AccessCondition.IfMatch(eTag); if (b[0] == 0 || Environment.TickCount - lastChange > 3000) // on ne peut garder un lock plus de 3 s { try { blob.UploadByteArray(b1, requestOpt); keepGoing = false; } catch (StorageClientException ex) { if (ex.ErrorCode != StorageErrorCode.ConditionFailed) throw; } } else Thread.Sleep(50); // constante arbitraire } while (keepGoing); }
internal static void MoveBlob(CloudBlob blob, string topath = "", string currentpath = "") { // Retrieve stroage account from connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")); // Create the blob client CloudBlobClient client = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = null; CloudBlobDirectory toFolder = null; CloudBlob newblob = null; if (topath.Contains('/')) { toFolder = client.GetBlobDirectoryReference(topath); string filepath = blob.Name.Substring(blob.Name.IndexOf("/") + 1); newblob = toFolder.GetBlobReference(filepath); } else { blobContainer = client.GetContainerReference(topath); string filepath = blob.Name.Substring(blob.Name.IndexOf("/") + 1); newblob = blobContainer.GetBlobReference(filepath); } newblob.UploadByteArray(blob.DownloadByteArray()); blob.DeleteIfExists(); }