static void Main(string[] args) { EnableQuickEditMode(); // Header. Console.WriteLine(""); Console.WriteLine(" Windows Azure Page Blob Breaklease Tool Ver 1.0 - George He"); Console.WriteLine(" Usage: breaklease <accountName> <accountKey> <page blob url>"); Begin: ConsoleColor colorFore = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Input your script, press enter to run:"); Console.ForegroundColor = colorFore; try { args = Console.ReadLine().Replace("breaklease ", "").Split(new char[] { ' ' }); // Validate args. if (args.Length != 3) { Console.WriteLine(" Invalid number of arguments."); Console.WriteLine(""); goto Begin; } var uri = args[2]; Console.WriteLine("Breaklease Processing: {0}", uri); Console.WriteLine(""); if (credentials == null || args[0] != initAccount) { initAccount = args[0]; credentials = new StorageCredentials(args[0], args[1]); storageAccount = new CloudStorageAccount(credentials, new Uri(string.Format("http://{0}.blob.core.chinacloudapi.cn/", args[0])), null, null, null); } var arrs = args[2].Split(new char[] { '/' }); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(arrs[3]); CloudBlockBlob blob = container.GetBlockBlobReference(arrs[4]); //Timespan is a period of time in seconds, here means 1 sec later break the lease after command execution. TimeSpan breakTime = new TimeSpan(0, 0, 1); blob.BreakLease(breakTime); Console.WriteLine("Breaklease successful, you can try to delete the file again!"); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: {0}", ex); Console.ForegroundColor = colorFore; } goto Begin; Console.ReadLine(); }
static void AcquireLeaseSample(string connectionString) { const string blobName = "rating.txt"; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient client = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = client.GetContainerReference("myfiles"); CloudBlockBlob blob = container.GetBlockBlobReference(blobName); blob.UploadText("0", Encoding.UTF8); Parallel.For(0, 20, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 2 }, i => { CloudStorageAccount sAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient bClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer bContainer = client.GetContainerReference("myfiles"); CloudBlockBlob blobRef = container.GetBlockBlobReference(blobName); bool isOk = false; while (isOk == false) { try { // The Lease Blob operation establishes and manages a lock on a blob for write and delete operations. // The lock duration can be 15 to 60 seconds, or can be infinite. string leaseId = blobRef.AcquireLease(TimeSpan.FromSeconds(15), Guid.NewGuid().ToString()); using (Stream stream = blobRef.OpenRead()) using (StreamReader reader = new StreamReader(stream)) { int raitingCount = int.Parse(reader.ReadToEnd()); byte[] bytesToUpload = Encoding.UTF8.GetBytes((raitingCount + 1).ToString(CultureInfo.InvariantCulture)); blobRef.UploadFromByteArray(bytesToUpload, 0, bytesToUpload.Length, AccessCondition.GenerateLeaseCondition(leaseId)); } blobRef.BreakLease(breakPeriod: TimeSpan.Zero, accessCondition: AccessCondition.GenerateLeaseCondition(leaseId)); isOk = true; } catch (Exception ex) { Console.WriteLine(ex.Message); } } }); }
public static void ForceUnlockAzureDirectory(CloudStorageAccount cloudStorageAccount, string container, TextWriter log = null) { log = log ?? DefaultTraceWriter; // unlocks the write.lock object - this should only be used after a system crash and only form a singleton CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(container); CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("write.lock"); try { log.WriteLine("About to attempt to BreakLease"); cloudBlockBlob.BreakLease(TimeSpan.FromMilliseconds(1)); log.WriteLine("BreakLease Success"); } catch (StorageException e) { log.WriteLine("BreakLease Exception"); // we will get a 409 "Conflict" if the lease is not there - ignore that case as all we were trying to do was drop the lease anyhow if (e.InnerException is WebException) { HttpWebResponse response = (HttpWebResponse)((WebException)e.InnerException).Response; if (response.StatusCode != HttpStatusCode.Conflict) { throw; } log.WriteLine("BreakLease Exception is harmless"); } else { throw; } } Thread.Sleep(3000); }