|
| 1 | +using Microsoft.WindowsAzure.Storage; |
| 2 | +using Microsoft.WindowsAzure.Storage.Blob; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace DataBlobStorageSample |
| 10 | +{ |
| 11 | + public class BlobLeases |
| 12 | + { |
| 13 | + |
| 14 | + |
| 15 | + //**********Tamra -- what about page blobs? Can we genericize this? |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Acquire a lease on the blob for the requested time. |
| 19 | + /// </summary> |
| 20 | + /// <param name="TimeInSeconds">Amount of time to hold the lease. This can be 15 to 60 seconds. If set to 0, the lease is infinite.</param> |
| 21 | + /// <param name="cloudBlockBlob">The blob on which to take out the lease.</param> |
| 22 | + /// <returns>Lease ID</returns> |
| 23 | + public string AcquireLease(int TimeInSeconds, CloudBlockBlob cloudBlockBlob) |
| 24 | + { |
| 25 | + |
| 26 | + if (TimeInSeconds > 0 && (TimeInSeconds < 15 || TimeInSeconds > 60)) |
| 27 | + { |
| 28 | + throw new ApplicationException("Time for the lease must be 0 (infinite) or between 15 and 60 seconds."); |
| 29 | + } |
| 30 | + |
| 31 | + //set the lease time -- it's a timespan object |
| 32 | + TimeSpan? leaseTime = null; |
| 33 | + if (TimeInSeconds > 0) |
| 34 | + { |
| 35 | + //set the lease time to the number of seconds passed in -- it's a timespan object |
| 36 | + leaseTime = TimeSpan.FromSeconds(TimeInSeconds); |
| 37 | + } |
| 38 | + |
| 39 | + //acquire the actual lease and return the lease ID |
| 40 | + string leaseID = cloudBlockBlob.AcquireLease(leaseTime, null); |
| 41 | + |
| 42 | + //after running this code, you should get these results: |
| 43 | + // LeaseDuration = Fixed; LeaseState = Leased; LeaseStatus = Locked |
| 44 | + //after the lease expires, you should get these results: |
| 45 | + // LeaseDuration = Unspecified, LeaseState = Expired, LeaseStatus = Unlocked |
| 46 | + |
| 47 | + return leaseID; |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// You must have the LeaseId of the blob in order to renew the lease. |
| 52 | + /// You can renew the lease even if it has expired, as long as the blob has not been modified or leased again since it expired. |
| 53 | + /// You can't change the duration when you renew the lease; it will use the previous lease duration. |
| 54 | + /// If you want to change the duration of the lease, use AcquireLease instead. |
| 55 | + /// </summary> |
| 56 | + /// <param name="cloudBlockBlob">Blob with the lease to be renewed</param> |
| 57 | + /// <param name="leaseId">current Lease Id</param> |
| 58 | + public void RenewLease(CloudBlockBlob cloudBlockBlob, string leaseId) |
| 59 | + { |
| 60 | + AccessCondition acc = new AccessCondition(); |
| 61 | + acc.LeaseId = leaseId; |
| 62 | + cloudBlockBlob.RenewLease(acc); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Change the leaseId of the current active lease. |
| 67 | + /// Must provide current leaseId and new leaseId. |
| 68 | + /// An example where you might use this is where the lease ownership needs to be transferred from one component of your system to another. |
| 69 | + /// For example, the first component has a lease on a blob, but needs to allow another component to operate on it. |
| 70 | + /// The first component could pass the Lease Id to the second component. |
| 71 | + /// The second component could change the Lease Id, do what it needs to do, then change it back to the original Lease Id. |
| 72 | + /// This allows the second component to temporarily have exclusive access to the blob, |
| 73 | + /// prevents the first component from modifying it while it has exclusive access, |
| 74 | + /// and then returns access to the first component. |
| 75 | + /// </summary> |
| 76 | + /// <param name="cloudBlockBlob">Blob with the lease</param> |
| 77 | + /// <param name="leaseId">The current lease Id</param> |
| 78 | + /// <returns>The new lease Id</returns> |
| 79 | + public string ChangeLease(CloudBlockBlob cloudBlockBlob, string leaseId) |
| 80 | + { |
| 81 | + AccessCondition acc = new AccessCondition(); |
| 82 | + acc.LeaseId = leaseId; |
| 83 | + string proposedLeaseId = System.Guid.NewGuid().ToString(); |
| 84 | + string newLeaseId = cloudBlockBlob.ChangeLease(proposedLeaseId, acc); |
| 85 | + return newLeaseId; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// To release the lease on a blob, you can either let it expire or call |
| 91 | + /// ReleaseLease to make it available immediately available for another client to lease it. |
| 92 | + /// You must have the current lease Id to release the lease. |
| 93 | + /// </summary> |
| 94 | + /// <param name="cloudBlockBlob">Blob with the lease on it</param> |
| 95 | + /// <param name="leaseId">Lease Id of the current lease that you want to release</param> |
| 96 | + public void ReleaseLease(CloudBlockBlob cloudBlockBlob, string leaseId) |
| 97 | + { |
| 98 | + AccessCondition acc = new AccessCondition(); |
| 99 | + acc.LeaseId = leaseId; |
| 100 | + cloudBlockBlob.ReleaseLease(acc); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Break the lease; does not require a lease Id. |
| 105 | + /// If you do this, the lease on the blob cannot be renewed. |
| 106 | + /// When you break a lease, you have to specify a timespan called the lease break period. |
| 107 | + /// During this period, no lease methods except for Break or Release can be performed. |
| 108 | + /// When you break the lease on a blob successfully, the response indicates the time interval |
| 109 | + /// in seconds until a new lease can be acquired. |
| 110 | + /// If one process breaks the lease on a blob and the original process that had the lease on the blob releases it, |
| 111 | + /// another client can immediately acquire a new lease, rather than waiting for the lease break period time to elapse. |
| 112 | + /// </summary> |
| 113 | + /// <param name="cloudBlockBlob"></param> |
| 114 | + public void BreakLease(CloudBlockBlob cloudBlockBlob, int breakReleaseTimeInSeconds) |
| 115 | + { |
| 116 | + //set the break release time |
| 117 | + TimeSpan? breakReleaseTime = TimeSpan.FromSeconds(breakReleaseTimeInSeconds); |
| 118 | + cloudBlockBlob.BreakLease(breakReleaseTime); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// The blob's Properties properties has information about the lease. |
| 124 | + /// Fetch the attributes to populate the properties and then display them. |
| 125 | + /// </summary> |
| 126 | + /// <param name="cloudBlockBlob">Blob for which to display the information.</param> |
| 127 | + public void DisplayLeaseProperties(CloudBlockBlob cloudBlockBlob, string leaseId) |
| 128 | + { |
| 129 | + //fetch attributes to populate the cloudBlockBlob.Properties properties |
| 130 | + // and display the properties we're interested in |
| 131 | + cloudBlockBlob.FetchAttributes(); |
| 132 | + |
| 133 | + //if a lease Id is supplied, print it |
| 134 | + if (!string.IsNullOrWhiteSpace(leaseId)) |
| 135 | + { |
| 136 | + Console.WriteLine(" LeaseId = {0}", leaseId); |
| 137 | + } |
| 138 | + //display the rest of the lease properties |
| 139 | + Console.WriteLine(" LeaseDuration = {0}", cloudBlockBlob.Properties.LeaseDuration); |
| 140 | + Console.WriteLine(" LeaseState = {0}", cloudBlockBlob.Properties.LeaseState); |
| 141 | + Console.WriteLine(" LeaseStatus = {0}", cloudBlockBlob.Properties.LeaseStatus); |
| 142 | + Console.WriteLine(string.Empty); |
| 143 | + } |
| 144 | + |
| 145 | + } |
| 146 | +} |
0 commit comments