Skip to content

Commit 6e27aab

Browse files
committed
Merge pull request #1 from robinsh/robinsh-0915
Added snapshots and leases samples.
2 parents 2f78890 + 67dbb48 commit 6e27aab

File tree

6 files changed

+562
-11
lines changed

6 files changed

+562
-11
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.suo
2+
BlobStorage/bin/Debug/BlobStorage.exe
3+
*.dll
4+
*.csproj
5+
*.xml
6+
BlobStorage/obj/Debug/*.*
7+
BlobStorage/bin/Debug/*.*
8+
*.nupkg
9+
packages/Newtonsoft.Json.5.0.8/tools/install.ps1

BlobStorage/App.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<!-- By default we are assuming you will use the Azure SDK Storage Emulator. If you have an Azure Subscription you can alternatively
55
create a Storage Account and run against the storage service by commenting out the connection string below and using the
66
second connection string - in which case you must also insert your storage account name and key in the line below. -->
7-
<add key="StorageConnectionString" value="UseDevelopmentStorage=true;" />
8-
<!--<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=[AccountName];AccountKey=[AccountKey]" />-->
7+
<add key="StorageConnectionString" value="UseDevelopmentStorage=true;" />
8+
<!--<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=PUTYOURACCOUNTNAMEHERE;AccountKey=PUTYOURACCOUNTKEYHERE" />-->
99
</appSettings>
1010
</configuration>

BlobStorage/BlobLeases.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
}

BlobStorage/BlobSnapshots.cs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using Microsoft.WindowsAzure.Storage.Blob;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace DataBlobStorageSample
7+
{
8+
public class BlobSnapshots
9+
{
10+
//The snapshot commands can also be run on a page blob, just change the incoming type accordingly.
11+
12+
/// <summary>
13+
/// Retrieve all of the snapshots and the base blob, iterate through them
14+
/// </summary>
15+
/// <param name="cloudBlobContainer">Container in which the blob resides</param>
16+
/// <param name="blobName">Name of the blob</param>
17+
public void ListSnapshotsAndProperties(CloudBlobContainer cloudBlobContainer, string blobName)
18+
{
19+
Console.WriteLine("***** List Snapshots and their Properties for a blob *****");
20+
21+
//get list of blobs and snapshots
22+
IEnumerable<IListBlobItem> listOfBlobs = GetListOfSnapshots(cloudBlobContainer, blobName);
23+
24+
foreach (IListBlobItem blobItem in listOfBlobs)
25+
{
26+
//you must cast this as a CloudBlockBlob
27+
// because blobItem does not expose all of the properties
28+
CloudBlockBlob theBlob = blobItem as CloudBlockBlob;
29+
30+
//Call FetchAttributes so it retrieves the metadata.
31+
theBlob.FetchAttributes();
32+
33+
//print the snapshot information
34+
Console.WriteLine("theBlob IsSnapshot = {0}, SnapshotTime = {1}, snapshotURI = {2}",
35+
theBlob.IsSnapshot, theBlob.SnapshotTime, theBlob.SnapshotQualifiedUri);
36+
37+
//iterate through the metadata and display each key-value pair
38+
int index = 0;
39+
foreach (KeyValuePair<string, string> kvPair in theBlob.Metadata)
40+
{
41+
index++;
42+
Console.WriteLine(".MetaData ({0}) = {1},{2}", index, kvPair.Key, kvPair.Value);
43+
}
44+
}
45+
}
46+
47+
/// <summary>
48+
/// Take a snapshot
49+
/// </summary>
50+
/// <param name="cloudBlockBlob">The blob for which to create the snapshot</param>
51+
/// <returns>The blob snapshot</returns>
52+
public CloudBlockBlob TakeASnapshot(CloudBlockBlob cloudBlockBlob)
53+
{
54+
//create the snapshot and return a reference to it
55+
CloudBlockBlob newBlob = cloudBlockBlob.CreateSnapshot();
56+
return newBlob;
57+
}
58+
59+
/// <summary>
60+
/// Get a list of IListBlobItems that are snapshots.
61+
/// </summary>
62+
/// <param name="cloudBlobContainer">Container in which the blobs reside</param>
63+
/// <param name="blobName">Name of the blob</param>
64+
/// <returns>List of snapshots for the specified blob</returns>
65+
public IEnumerable<IListBlobItem> GetListOfSnapshots(CloudBlobContainer cloudBlobContainer, string blobName)
66+
{
67+
IEnumerable<IListBlobItem> listOfBlobItems = cloudBlobContainer.ListBlobs(blobName, true, BlobListingDetails.Snapshots);
68+
return listOfBlobItems;
69+
}
70+
71+
/// <summary>
72+
/// Copy a snapshot to a new blob.
73+
/// </summary>
74+
/// <param name="sourceBlob">The snapshot to be copied</param>
75+
/// <param name="cloudBlobContainer">The container to which the snapshot should be copied</param>
76+
/// <param name="newBlobName">The name of the new blob</param>
77+
public void CopySnapshotToBlob(CloudBlockBlob sourceBlob, CloudBlobContainer cloudBlobContainer, string newBlobName)
78+
{
79+
CloudBlockBlob blobTarget = cloudBlobContainer.GetBlockBlobReference(newBlobName);
80+
blobTarget.StartCopyFromBlob(sourceBlob);
81+
}
82+
83+
/// <summary>
84+
/// Delete all snapshots for a blob.
85+
/// </summary>
86+
/// <param name="cloudBlockBlob">The blob whose snapshots are to be deleted</param>
87+
public void DeleteAllSnapshots(CloudBlockBlob cloudBlockBlob)
88+
{
89+
cloudBlockBlob.Delete(DeleteSnapshotsOption.DeleteSnapshotsOnly);
90+
}
91+
92+
/// <summary>
93+
/// Delete the snapshots and the blob.
94+
/// </summary>
95+
/// <param name="cloudBlockBlob">The blob to be fully removed</param>
96+
public void DeleteSnapshotsAndBlob(CloudBlockBlob cloudBlockBlob)
97+
{
98+
cloudBlockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
99+
}
100+
101+
/// <summary>
102+
/// Delete the blob, but only if it has no snapshots.
103+
/// </summary>
104+
/// <param name="cloudBlockBlob">The blob to be deleted</param>
105+
public void DeleteBlobIfItHasNoSnapshots(CloudBlockBlob cloudBlockBlob)
106+
{
107+
cloudBlockBlob.Delete(DeleteSnapshotsOption.None);
108+
}
109+
}
110+
}

BlobStorage/BlobStorage.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
<Reference Include="System.Xml" />
7272
</ItemGroup>
7373
<ItemGroup>
74+
<Compile Include="BlobLeases.cs" />
75+
<Compile Include="BlobSnapshots.cs" />
7476
<Compile Include="Program.cs" />
7577
<Compile Include="Properties\AssemblyInfo.cs" />
7678
</ItemGroup>

0 commit comments

Comments
 (0)