Skip to content

Commit 1fe8ad7

Browse files
author
perrysk-msft
committed
Adding files
2 parents 3e48d59 + e4b8cc0 commit 1fe8ad7

15 files changed

+1296
-26
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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
10+
*.ps1
11+
packages/WindowsAzure.Storage.5.0.2/nuget.exe

BlobStorage/App.config

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
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>
10+
<runtime>
11+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
12+
<dependentAssembly>
13+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
15+
</dependentAssembly>
16+
</assemblyBinding>
17+
</runtime>
1018
</configuration>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.WindowsAzure;
7+
using Microsoft.WindowsAzure.Storage;
8+
using Microsoft.WindowsAzure.Storage.Blob;
9+
10+
namespace BlobStorage
11+
{
12+
class BlobContainerOperations
13+
{
14+
15+
async public static Task RunContainerOperations()
16+
{
17+
// Retrieve the connection string from the app.config file.
18+
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));
19+
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
20+
21+
// Create a randomly named container for sample purposes.
22+
CloudBlobContainer container = blobClient.GetContainerReference("sample-container" + DateTime.Now.Ticks);
23+
24+
//Create the container if it does not already exist.
25+
await container.CreateIfNotExistsAsync();
26+
27+
await AddContainerMetadata(container);
28+
29+
await ListContainerPropertiesAndMetadata(container);
30+
31+
// Uncomment to delete the sample container at the end of the sample.
32+
//await container.DeleteAsync();
33+
}
34+
35+
36+
async public static Task AddContainerMetadata(CloudBlobContainer container)
37+
{
38+
// Add some metadata to the container.
39+
container.Metadata.Add("docType", "textDocuments");
40+
container.Metadata["category"] = "guidance";
41+
42+
// Set the container's metadata asynchronously.
43+
await container.SetMetadataAsync();
44+
}
45+
46+
47+
async public static Task ListContainerPropertiesAndMetadata(CloudBlobContainer container)
48+
{
49+
// You must first fetch the container's attributes in order to
50+
// populate the container's properties and metadata.
51+
await container.FetchAttributesAsync();
52+
53+
// Write out container property values.
54+
ReflectionTools.PrintTypeProperties(container);
55+
ReflectionTools.PrintTypeProperties(container.Properties);
56+
57+
// Enumerate the container's metadata.
58+
Console.WriteLine("Container metadata:");
59+
foreach (var metadataItem in container.Metadata)
60+
{
61+
Console.WriteLine("\tKey: {0}", metadataItem.Key);
62+
Console.WriteLine("\tValue: {0}", metadataItem.Value);
63+
}
64+
Console.WriteLine();
65+
}
66+
67+
68+
69+
}
70+
}

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+
}

0 commit comments

Comments
 (0)