Azure

From Slothx.net

Jump to: navigation, search

Contents

General

Articles

Downloads

C# Code Samples

Azure Table Code

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace Azure_Android_Data_Test1
{
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    using Microsoft.WindowsAzure.Storage.Auth;

    using System.Threading.Tasks;
    using System.Collections.Generic;

    // Azure Table Data ---------------------------------------

    public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string partition_key, string row_key)
        {
            this.PartitionKey = partition_key;
            this.RowKey = row_key;
        }
        public string Name { get; set; }
    }

    // Azure IO Routines --------------------------------------

    public class Azure_IO
    {
        CloudTable cloud_table = null;

        async public Task<bool> fnInsert_Record(ITableEntity table_record)
        {
            try
            {
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(table_record);

                await cloud_table.ExecuteAsync(insertOrMergeOperation);

                return true;

            }
            catch
            {
                return false;
            }
        }

        async public Task<bool> fnInsert_Records(List<ITableEntity> table_record_list)
        {
            try
            {
                TableBatchOperation batchOperation = new TableBatchOperation();

                foreach (ITableEntity table_record in table_record_list)
                {
                    batchOperation.InsertOrMerge(table_record);
                }

                await cloud_table.ExecuteBatchAsync(batchOperation);

                return true;

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public void fnOpen_Table(string table_name)
        {
            try
            {
                string azure_connection = "UseDevelopmentStorage=true";

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azure_connection);
                CloudTableClient table_client = storageAccount.CreateCloudTableClient();
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table(string storage_account_name,
                 string storage_account_key,
                 string table_name)
        {
            try
            {
                string azure_connection = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                             storage_account_name,
                                             storage_account_key);

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azure_connection);
                CloudTableClient table_client = storageAccount.CreateCloudTableClient();
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table_via_SAS(string storage_account_name,
                         string shared_access_signature,
                         string table_name)
        {
            try
            {
                StorageCredentials creds = new StorageCredentials(shared_access_signature);

                string endpoint = null;

                if (storage_account_name == "")
                {
                    endpoint = string.Format("http://127.0.0.1:10002/devstoreaccount1/{0}", table_name);
                }
                else
                {
                    endpoint = string.Format("https://{0}.table.core.windows.net", storage_account_name);
                }
                CloudTableClient table_client = new CloudTableClient(new Uri(endpoint), creds);
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

    }

    // Test Classes

    public class Azure_Storage_Emulator_Test
    {
        async public Task<bool> fnSave_Data(string table_name,
                                      string partition_key,
                                      int instance,
                                      int number_of_records)
        {

            string row_key = null;
            bool result = false;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table(table_name);

            List<ITableEntity> customer_list = new List<ITableEntity>();

            partition_key = partition_key + instance.ToString("00");

            for (int i = 1; i <= number_of_records; i++)
            {
                row_key = i.ToString("000");
                CustomerEntity customer = new CustomerEntity(partition_key, row_key);
                customer.Name = row_key;
                customer_list.Add(customer);
            }

            CustomerEntity customer_single = new CustomerEntity("A-emulator-single", "999");
            bool records_saved1 = await my_io.fnInsert_Record(customer_single);

            bool records_saved2 = await my_io.fnInsert_Records(customer_list);

            if (records_saved1 == true && records_saved2 == true)
            {
                result = true;
            }
            else
            {
                result = false;
            }

            return result;

        }

    }

    public class Azure_Storage_Test
    {
        async public Task<bool> fnSave_Data(string table_name,
                                      string partition_key,
                                      int instance,
                                      int number_of_records,
                                      string storage_account_name,
                                      string storage_account_key)
        {
            bool result;
            string row_key = null;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table(storage_account_name, storage_account_key, table_name);

            List<ITableEntity> customer_list = new List<ITableEntity>();

            partition_key = partition_key + instance.ToString("00");

            for (int i = 1; i <= number_of_records; i++)
            {
                row_key = i.ToString("000");
                CustomerEntity customer = new CustomerEntity(partition_key, row_key);
                customer.Name = row_key;
                customer_list.Add(customer);
            }

            CustomerEntity customer_single = new CustomerEntity("A-Storage-single", "999");
            bool records_saved1 = await my_io.fnInsert_Record(customer_single);

            bool records_saved2 = await my_io.fnInsert_Records(customer_list);

            if (records_saved1 == true && records_saved2 == true)
            {
                result = true;
            }
            else
            {
                result = false;
            }

            return result;

        }

        async public Task<bool> fnSave_Data_via_SAS(string table_name,
                                      string partition_key,
                                      int instance,
                                      int number_of_records,
                                      string storage_account_name,
                                      string shared_access_token)
        {

            string row_key = null;
            bool result;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table_via_SAS(storage_account_name, shared_access_token, table_name);

            List<ITableEntity> customer_list = new List<ITableEntity>();

            partition_key = partition_key + instance.ToString("00");

            for (int i = 1; i <= number_of_records; i++)
            {
                row_key = i.ToString("000");
                CustomerEntity customer = new CustomerEntity(partition_key, row_key);
                customer.Name = row_key;
                customer_list.Add(customer);
            }

            CustomerEntity customer_single = new CustomerEntity("A-Sas-single", "001");
            bool records_saved1 = await my_io.fnInsert_Record(customer_single);

            bool records_saved2 = await my_io.fnInsert_Records(customer_list);

            if (records_saved1 == true && records_saved2 == true)
            {
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }
    }
}


Storage Emulator Simple Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Azure_Storage_Emulator_Simple_Test
{
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    using Microsoft.WindowsAzure.Storage.Auth;

    // Azure Table Data ---------------------------------------

    public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string partition_key, string row_key)
        {
            this.PartitionKey = partition_key;
            this.RowKey = row_key;
        }
        public string Name { get; set; }
    }

    // Azure IO Routines --------------------------------------

    public class Azure_IO
    {
        CloudTable cloud_table = null;

        async public Task fnInsert_Records(List<ITableEntity> table_record_list)
        {
            try
            {
                TableBatchOperation batchOperation = new TableBatchOperation();

                foreach (ITableEntity table_record in table_record_list)
                {
                    batchOperation.InsertOrMerge(table_record);
                }

                await cloud_table.ExecuteBatchAsync(batchOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table(string table_name)
        {
            try
            {
                string azure_connection = "UseDevelopmentStorage=true";

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azure_connection);
                CloudTableClient table_client = storageAccount.CreateCloudTableClient();
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    // Test Classes

    public class Azure_Storage_Emulator_Test
    {
        public async void fnSave_Data(int counter)
        {
            string table_name = "customer";

            string instance = "Emulator:" + counter.ToString("000");
            string row_key = null;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table(table_name);

            List<ITableEntity> customer_list = new List<ITableEntity>();

            for (int i = 1; i <= 100; i++)
            {
                row_key = i.ToString("000");
                CustomerEntity customer = new CustomerEntity(instance, row_key);
                customer.Name = row_key;
                customer_list.Add(customer);
            }

            try
            {
                await my_io.fnInsert_Records(customer_list);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }
    }


    // Mainline -----------------------------------------------

    class Program
    {
        static void Main(string[] args)
        {

            Azure_Storage_Emulator_Test driver1 = new Azure_Storage_Emulator_Test();

            for (int i = 1; i <= 100; i++)
            {
                driver1.fnSave_Data(i);
            }
        }
    }
}


Storage Emulator Test

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Install-Package WindowsAzure.Storage <-- To setup project.

namespace Azure_Storage_Emulator_Test
{
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    using Microsoft.WindowsAzure.Storage.Auth;

    // Azure Table Data ---------------------------------------

    public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string partition_key, string row_key)
        {
            this.PartitionKey = partition_key;
            this.RowKey = row_key;
        }
        public string Name { get; set; }
    }

    // Azure IO Routines --------------------------------------

    public class Azure_IO
    {
        CloudTable cloud_table = null;

        async public Task fnInsert_Record(ITableEntity table_record)
        {
            try
            {
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(table_record);

                await cloud_table.ExecuteAsync(insertOrMergeOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        async public Task fnInsert_Records(List<ITableEntity> table_record_list)
        {
            try
            {
                TableBatchOperation batchOperation = new TableBatchOperation();

                foreach (ITableEntity table_record in table_record_list)
                {
                    batchOperation.Insert(table_record);
                }

                await cloud_table.ExecuteBatchAsync(batchOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table(string storage_account_name,
                         string storage_account_key,
                         string table_name)
        {
            try
            {
                string azure_connection = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                             storage_account_name,
                                             storage_account_key);

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azure_connection);
                CloudTableClient table_client = storageAccount.CreateCloudTableClient();
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table(string table_name)
        {
            try
            {
                string azure_connection = "UseDevelopmentStorage=true";

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azure_connection);
                CloudTableClient table_client = storageAccount.CreateCloudTableClient();
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    // Test Classes

    public class Azure_Storage_Emulator_Test
    {
        public void fnSave_Data()
        {
            string table_name = "customer";

            string row_key = null;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table(table_name);

            List<ITableEntity> customer_list = new List<ITableEntity>();

            for (int i = 1; i <= 10; i++)
            {
                row_key = i.ToString("000");
                CustomerEntity customer = new CustomerEntity("Emualator", row_key);
                customer.Name = row_key;
                customer_list.Add(customer);
            }

            my_io.fnInsert_Records(customer_list).Wait();
        }
    }

    public class Azure_Storage_Test
    {
        public void fnSave_Data()
        {
            string storage_account_name = "";
            string storage_account_key = "";
            string table_name = "customer";

            string row_key = null;

            Azure_IO my_io = new Azure_IO();

            List<ITableEntity> customer_list = new List<ITableEntity>();

            my_io.fnOpen_Table(storage_account_name,
                                   storage_account_key,
                                   table_name);
            for (int i = 1; i <= 10; i++)
            {
                row_key = i.ToString("000");
                CustomerEntity customer = new CustomerEntity("Storage", row_key);
                customer.Name = row_key;
                customer_list.Add(customer);
            }

            my_io.fnInsert_Records(customer_list).Wait();
        }
    }

    // Mainline -----------------------------------------------

    class Program
    {
        static void Main(string[] args)
        {
            Azure_Storage_Test driver1 = new Azure_Storage_Test();
            driver1.fnSave_Data();

            Azure_Storage_Emulator_Test driver2 = new Azure_Storage_Emulator_Test();

            driver2.fnSave_Data();
        }
    }
}


Basic Async Table Insert

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Install-Package WindowsAzure.Storage <-- To setup project.

namespace Azure_Basic_Async_Table_Insert
{
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    using Microsoft.WindowsAzure.Storage.Auth;

    // Azure Table Data ---------------------------------------

    public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string lastName, string firstName)
        {
            this.PartitionKey = lastName;
            this.RowKey = firstName;
        }
        public string Name { get; set; }
    }

    // Azure IO Routines --------------------------------------

    public class Azure_IO
    {
        CloudTable cloud_table = null;

        async public Task fnInsert_Record(ITableEntity table_record)
        {
            try
            {
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(table_record);

                await cloud_table.ExecuteAsync(insertOrMergeOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table(string storage_account_name,
                                 string storage_account_key,
                                 string table_name)
        {
            try
            {
                string azure_connection = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                             storage_account_name,
                                             storage_account_key);

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(azure_connection);
                CloudTableClient table_client = storageAccount.CreateCloudTableClient();
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    // Mainline -----------------------------------------------

    class Program
    {
        static void Main(string[] args)
        {
            string storage_account_name = "...";
            string storage_account_key  = "...";
            string table_name = "customer";

            string customer_name = null;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table(storage_account_name,
                               storage_account_key,
                               table_name);

            for (int i=1;i<=10;i++)
            {
                customer_name = "Customer:" + i.ToString();
                CustomerEntity customer = new CustomerEntity(customer_name, customer_name);
                customer.Name = customer_name;
                my_io.fnInsert_Record(customer).Wait();
            }
        }
    }
}

Azure Table SAS Generator

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Install-Package WindowsAzure.Storage <-- To setup project.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Auth;

namespace Azure_Table_SAS_Generator
{
    public class Azuze_Table_SAS
    {
        public string fnGenerate_Table_SAS(string storage_account_name,
                                           string storage_account_key,
                                           string table_name,
                                           int hours,
                                           int minutes,
                                           int seconds)
        {
            string azure_connection = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                         storage_account_name,
                                         storage_account_key);
            string shared_access_token;

            try
            {
                CloudStorageAccount account = CloudStorageAccount.Parse(azure_connection);

                CloudTableClient tableClient = account.CreateCloudTableClient();
                CloudTable table = tableClient.GetTableReference(table_name);

                SharedAccessTablePolicy sasPolicy = new SharedAccessTablePolicy();

                sasPolicy.Permissions = SharedAccessTablePermissions.Query |
                                        SharedAccessTablePermissions.Add |
                                        SharedAccessTablePermissions.Update |
                                        SharedAccessTablePermissions.Delete;
                sasPolicy.SharedAccessExpiryTime = DateTime.UtcNow.Add(new TimeSpan(hours,
                                                                                   minutes,
                                                                                   seconds));

                shared_access_token = table.GetSharedAccessSignature(sasPolicy);

                return shared_access_token;

            }

            catch (Exception ex)
            {
                throw ex;
            }

        }

    }
    class Program
    {

        [STAThread]
        static void Main(string[] args)
        {

            string storage_account_name;
            string storage_account_key;
            string table_name;

            int hours = 5;
            int minutes = 0;
            int seconds = 0;

            storage_account_name = "...";
            storage_account_key =  "...";
            table_name = "customer";

            Azuze_Table_SAS my_sas = new Azuze_Table_SAS();

            string shared_access_token = my_sas.fnGenerate_Table_SAS(storage_account_name,
                                                                     storage_account_key,
                                                                     table_name,
                                                                     hours,
                                                                     minutes,
                                                                     seconds);

            System.Windows.Forms.Clipboard.SetText(shared_access_token);
        }
    }
}

Azure Basic Table Async Insert via SAS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// Install-Package WindowsAzure.Storage <-- To setup project.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Auth;

namespace Azure_Test_Fragment
{
    // Azure Table Data ---------------------------------------

    public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string partition_key, string row_key)
        {
            this.PartitionKey = partition_key;
            this.RowKey = row_key;
        }
        public string Name { get; set; }
    }

    // Azure IO Routines --------------------------------------

    public class Azure_IO
    {
        CloudTable cloud_table = null;

        async public Task fnInsert_Record(ITableEntity table_record)
        {
            try
            {
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(table_record);

                await cloud_table.ExecuteAsync(insertOrMergeOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        async public Task fnInsert_Records(List<ITableEntity> table_record_list)
        {
            try
            {
                TableBatchOperation batchOperation = new TableBatchOperation();

                foreach (ITableEntity table_record in table_record_list)
                {
                    batchOperation.Insert(table_record);
                }

                await cloud_table.ExecuteBatchAsync(batchOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table(string storage_account_name,
                                 string shared_access_signature,
                                 string table_name)
        {
            try
            {
                StorageCredentials creds = new StorageCredentials(shared_access_signature);
                string endpoint = string.Format("https://{0}.table.core.windows.net", storage_account_name);
                CloudTableClient table_client = new CloudTableClient(new Uri(endpoint), creds);
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }

    // Mainline -----------------------------------------------

    class Program
    {        
        static void Main(string[] args)
        {
            string storage_account_name;
            string table_name;
            string shared_access_token;

            List<ITableEntity> customer_list = new List<ITableEntity>();

            storage_account_name = "...";            
            shared_access_token  = "...";
            table_name = "customer";
            string customer_name = null;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table(storage_account_name,
                               shared_access_token,
                               table_name);

            for (int i = 1; i <= 100; i++)
            {
                customer_name = "Data:" + i.ToString("000");
                CustomerEntity customer = new CustomerEntity("BatchInsert", customer_name);
                customer.Name = customer_name;
                customer_list.Add(customer);
            }

            my_io.fnInsert_Records(customer_list).Wait();
        }
    }
}

Test Fragment

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=391641

namespace Phone_Azure_Test
{
    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Table;
    using Microsoft.WindowsAzure.Storage.Auth;

    using System.Threading.Tasks;

    // Azure Table Data ---------------------------------------

    public class CustomerEntity : TableEntity
    {
        public CustomerEntity(string partition_key, string row_key)
        {
            this.PartitionKey = partition_key;
            this.RowKey = row_key;
        }
        public string Name { get; set; }
    }

    // Azure IO Routines --------------------------------------

    public class Azure_IO
    {
        CloudTable cloud_table = null;

        async public Task fnInsert_Record(ITableEntity table_record)
        {
            try
            {
                TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(table_record);

                await cloud_table.ExecuteAsync(insertOrMergeOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        async public Task fnInsert_Records(List<ITableEntity> table_record_list)
        {
            try
            {
                TableBatchOperation batchOperation = new TableBatchOperation();

                foreach (ITableEntity table_record in table_record_list)
                {
                    batchOperation.InsertOrReplace(table_record);
                }

                await cloud_table.ExecuteBatchAsync(batchOperation);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void fnOpen_Table(string storage_account_name,
                                 string shared_access_signature,
                                 string table_name)
        {
            try
            {
                StorageCredentials creds = new StorageCredentials(shared_access_signature);
                string endpoint = string.Format("https://{0}.table.core.windows.net", storage_account_name);
                CloudTableClient table_client = new CloudTableClient(new Uri(endpoint), creds);
                cloud_table = table_client.GetTableReference(table_name);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
    public class DataDriver
    {
        public void fnSave_Data()
        {
            string storage_account_name;
            string table_name;
            string shared_access_token;

            List<ITableEntity> customer_list = new List<ITableEntity>();

            storage_account_name = "...";
            shared_access_token  = "...";
            table_name = "customer";
            string customer_name = null;

            Azure_IO my_io = new Azure_IO();

            my_io.fnOpen_Table(storage_account_name,
                               shared_access_token,
                               table_name);

            for (int i = 1; i <= 100; i++)
            {
                customer_name = "Data:" + i.ToString("000");
                CustomerEntity customer = new CustomerEntity("BatchInsert", customer_name);
                customer.Name = customer_name;
                customer_list.Add(customer);
            }

            Task t1 = my_io.fnInsert_Records(customer_list);

            TaskStatus my_status = t1.Status;

            while (my_status != TaskStatus.RanToCompletion)
            {
                Task.Delay(TimeSpan.FromSeconds(1)).Wait();
                my_status = t1.Status;
            }

        }

    }

}




Site Changes

Site Navigation

Personal tools