private static void SetLength(CloudPageBlob blob, long newLength, ExponentialRetry retryPolicy) { TimeSpan?leaseTime = TimeSpan.FromSeconds(15); var options = new BlobRequestOptions { RetryPolicy = retryPolicy }; var leaseId = blob.AcquireLease(leaseTime, null, options: options); blob.Resize(newLength, new AccessCondition { LeaseId = leaseId }, options: options); blob.ReleaseLease(new AccessCondition { LeaseId = leaseId }, options: options); }
// prevent this blob from being deleted by acquiring a lease on it private void leaseButton_Click(object sender, EventArgs e) { String blobName = null; // get blob name if (blobList.SelectedItem == null) { MessageBox.Show("No blob selected.", "No blob"); return; } else { blobName = blobList.SelectedItem.ToString(); } // get container reference CloudBlobContainer container = blobClient.GetContainerReference(containerName); // get blob reference CloudPageBlob blob = container.GetPageBlobReference(blobName); // acquire lease try { string newLeaseId = Guid.NewGuid().ToString(); blob.AcquireLease(null, newLeaseId); MessageBox.Show("Lease " + newLeaseId + " acquired.", "Lease success"); } catch (Exception ex) { // check if there is already a lease on the blob - status code 409 StorageException storageException = ex as StorageException; if (storageException != null && storageException.RequestInformation.HttpStatusCode == 409) { DialogResult answer2 = MessageBox.Show("There already a lease on this blob", "Lease exists"); } else { MessageBox.Show("Error acquiring lease: " + ex.ToString(), ex.GetType().FullName); } } }