Thursday, July 11, 2013

Azure Table Storage F# most basic example

In case anyone else was as confused as I was.... while trying an experiment out with WindowsAzure Table Storage. Searching the net just keeps highlighting old documentation and with the API changing it had me going no where for quite awhile. Basic run through here:

#r @"C:\wd\AzureTxnUploader\packages\WindowsAzure.Storage.2.0.6.0\lib\net40\Microsoft.WindowsAzure.Storage.dll"
#r @"C:\wd\AzureTxnUploader\packages\Microsoft.Data.OData.5.2.0\lib\net40\Microsoft.Data.OData.dll"
#r @"C:\wd\AzureTxnUploader\packages\Microsoft.Data.Edm.5.2.0\lib\net40\Microsoft.Data.Edm.dll"
#r @"C:\wd\AzureTxnUploader\packages\System.Spatial.5.2.0\lib\net40\System.Spatial.dll"
#r "Microsoft.WindowsAzure.ServiceRuntime"
#r "System.Data.Services.Client"
#r "System.Linq"
open System
open Microsoft.WindowsAzure
open Microsoft.WindowsAzure.Storage
open System.Windows.Forms
// Account name and key. Modify for your account.
let accountName = "someAccountName"
let accountKey = "someAccountKey"
//Get a reference to the storage account, with authentication credentials
let credentials = new Auth.StorageCredentials(accountName, accountKey)
let storageAccount = new CloudStorageAccount(credentials, true)
storageAccount.BlobEndpoint = Uri("http://someAccountName.blob.core.windows.net/")
storageAccount.TableEndpoint = Uri("http://someAccountName.table.core.windows.net/")
storageAccount.QueueEndpoint = Uri("http://someAccountName.queue.core.windows.net/")
//Create a new client object.
let blobClient = storageAccount.CreateCloudBlobClient()
let tableClient = storageAccount.CreateCloudTableClient()
// Retrieve a reference to a container.
let container = blobClient.GetContainerReference("mycontainer")
// Create the container if it does not already exist.
container.CreateIfNotExists()
let table = tableClient.GetTableReference("someTable")
table.CreateIfNotExists()
// put some entities into the table
type YearMonthTxns() =
inherit Table.TableEntity()
member val txns = "" with get, set
let someYearMonthTxns = YearMonthTxns( PartitionKey = "2013", RowKey = "06", txns = "test" )
let cmd = Table.TableOperation.Insert(someYearMonthTxns)
let tableResult = table.Execute(cmd)
// Output container URI to debug window.
System.Diagnostics.Debug.WriteLine(container.Uri)

No comments: