Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created July 31, 2012 12:24
Show Gist options
  • Select an option

  • Save chgeuer/3216682 to your computer and use it in GitHub Desktop.

Select an option

Save chgeuer/3216682 to your computer and use it in GitHub Desktop.
Using a shared access signature for a table
using System;
using System.Data.Services.Client;
using System.Data.Services.Common;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace PlayingWithAzureTableSAS
{
class Program
{
static void Main(string[] args)
{
var accountName = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT");
var key = System.Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCESS_KEY");
var storageAccount = new CloudStorageAccount(new StorageCredentialsAccountAndKey(accountName, key),
useHttps: true);
var tablename = "mytable";
var tableClient = storageAccount.CreateCloudTableClient();
tableClient.CreateTableIfNotExist(tablename);
var table = tableClient.GetTableReference(tablename);
var policy = new SharedAccessTablePolicy()
{
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(3),
Permissions = SharedAccessTablePermissions.Query | SharedAccessTablePermissions.Add | SharedAccessTablePermissions.Update
};
string facebookUserId = "facebookUserId";
string sas = table.GetSharedAccessSignature(policy: policy,
accessPolicyIdentifier: null,
startPartitionKey: facebookUserId,
endPartitionKey: facebookUserId,
startRowKey: null,
endRowKey: null);
Console.WriteLine(sas);
WriteDataToTable(accountName, tablename, facebookUserId, sas);
var address = string.Format("{0}/{1}{2}", accountName, tablename, sas);
Console.WriteLine(address);
QueryDataToTable(accountName, tablename, facebookUserId, sas);
}
private static void WriteDataToTable(string accountName, string tablename, string facebookUserId, string sas)
{
var tableEndpoint = string.Format("https://{0}.table.core.windows.net", accountName);
var sasTableClient = new CloudTableClient(tableEndpoint, new StorageCredentialsSharedAccessSignature(sas));
var e = new Eintrag(facebookUserId, "some event " + DateTime.UtcNow.Ticks) { Zeugs = "noch mehr trallala " + DateTime.Now.ToString() };
var ctx = sasTableClient.GetDataServiceContext();
ctx.AddObject(tablename, e);
ctx.SaveChangesWithRetries(SaveChangesOptions.None);
}
private static void QueryDataToTable(string accountName, string tablename, string facebookUserId, string sas)
{
var tableEndpoint = string.Format("https://{0}.table.core.windows.net", accountName);
var sasTableClient = new CloudTableClient(tableEndpoint, new StorageCredentialsSharedAccessSignature(sas));
var ctx = sasTableClient.GetDataServiceContext();
var eintraege = ctx.CreateQuery<Eintrag>(tablename).ToList();
eintraege.ForEach(e => Console.WriteLine(e.RowKey + ": " + e.Zeugs));
}
}
[DataServiceKey("PartitionKey", "RowKey")]
public class Eintrag
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTime Timestamp { get; set; }
public string Zeugs { get; set; }
public Eintrag() { }
public Eintrag(string partitionKey, string rowKey)
{
PartitionKey = partitionKey;
RowKey = rowKey;
}
}
}
/*
POST https://chgeuer.table.core.windows.net/mytable?sv=2012-02-12&se=2012-07-31T15%3A26%3A57Z&tn=mytable&sp=rau&spk=facebookUserId&epk=facebookUserId&sig=BGH46%2BhDb6ybfIBlNCFMmWhnF9XFPiLVOCtIuT8R06g%3D HTTP/1.1
Host: chgeuer.table.core.windows.net
Content-Type: application/atom+xml
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
x-ms-version: 2012-02-12
User-Agent: Microsoft ADO.NET Data Services
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 2.0;NetFx
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title />
<author>
<name />
</author>
<updated>2012-07-31T12:26:57.4228506Z</updated>
<id />
<content type="application/xml">
<m:properties>
<d:PartitionKey>facebookUserId</d:PartitionKey>
<d:RowKey>some event 634793344174178506</d:RowKey>
<d:Timestamp m:type="Edm.DateTime">0001-01-01T00:00:00</d:Timestamp>
<d:Zeugs>noch mehr trallala 31.07.2012 14:26:57</d:Zeugs>
</m:properties>
</content>
</entry>
HTTP/1.1 201 Created
Cache-Control: no-cache
Transfer-Encoding: chunked
Content-Type: application/atom+xml;charset=utf-8
ETag: W/"datetime'2012-07-31T12%3A26%3A58.4566897Z'"
Location: https://chgeuer.table.core.windows.net/mytable(PartitionKey='facebookUserId',RowKey='some%20event%20634793344174178506')
Server: Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 3bbafafe-7746-4728-a23f-09d97533bda6
x-ms-version: 2012-02-12
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xml:base="https://chgeuer.table.core.windows.net/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:etag="W/&quot;datetime'2012-07-31T12%3A26%3A58.4566897Z'&quot;" xmlns="http://www.w3.org/2005/Atom">
<id>https://chgeuer.table.core.windows.net/mytable(PartitionKey='facebookUserId',RowKey='some%20event%20634793344174178506')</id>
<title type="text"></title>
<updated>2012-07-31T12:26:58Z</updated>
<author>
<name />
</author>
<link rel="edit" title="mytable" href="mytable(PartitionKey='facebookUserId',RowKey='some%20event%20634793344174178506')" />
<category term="chgeuer.mytable" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
<content type="application/xml">
<m:properties>
<d:PartitionKey>facebookUserId</d:PartitionKey>
<d:RowKey>some event 634793344174178506</d:RowKey>
<d:Timestamp m:type="Edm.DateTime">2012-07-31T12:26:58.4566897Z</d:Timestamp>
<d:Zeugs>noch mehr trallala 31.07.2012 14:26:57</d:Zeugs>
</m:properties>
</content>
</entry>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment