public
Created

Sample command-line utility to enable Storage Analytics on Windows Azure

  • Download Gist
Program.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Microsoft.WindowsAzure;
using System.IO;
using System.Xml.Linq;
using NDesk.Options;
 
namespace StorageAnalytics
{
class Program
{
static void Main(string[] args)
{
string account = null, accessKey = null, type = null;
bool getOperation = false, listOperation = false;
string filePath = null;
StorageCredentialsAccountAndKey creds;
 
// Define command-line arguments
 
var p = new OptionSet()
{
{ "a|account=", "{account} name", v => account = v },
{ "k|key=", "access {key}", v => accessKey = v},
{ "t|type=", "storage {type}: 'blob' or 'table'", v => type = v},
{ "g|get", "get current configuration", v => getOperation = true },
{ "s|set=", "set current configuration from {file}", v => filePath = v },
{ "l|list", "list contents of $logs container", v => listOperation = true }
};
 
// Parse arguments
 
try
{
p.Parse(args);
}
catch (OptionException e)
{
Usage(p, e.Message);
return;
}
 
// Check the arguments
 
if (account == null || accessKey == null || type == null || (type != "blob" && type != "table"))
{
Usage(p, "account, accesskey and type are mandatory; type must be 'blob' or 'table'");
return;
}
 
// Create storage credentials
 
try
{
creds = new StorageCredentialsAccountAndKey(account, accessKey);
}
catch (Exception e)
{
Console.WriteLine("failed to create storage credentials: " + e.Message);
Console.WriteLine("please check account and accesskey!");
return;
}
 
// Execute the operation
 
if (getOperation)
{
DoGet(account, type, "?restype=service&comp=properties", creds);
return;
}
else if (listOperation)
{
DoGet(account, type, "$logs/?restype=container&comp=list", creds);
return;
}
else if (!String.IsNullOrEmpty(filePath))
{
DoPut(account, type, "?restype=service&comp=properties", creds, filePath);
return;
}
else
{
Usage(p, "please specify either -get or -set operation");
}
}
 
// Send a GET REST API request & print the response body
 
private static void DoGet(string account, string type, string query, StorageCredentials creds)
{
HttpWebRequest req;
HttpWebResponse resp;
req = CreateRequest(account, type, query, "GET", creds, null);
 
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
Console.WriteLine(e.Message);
return;
}
 
Console.WriteLine(XDocument.Parse(
new StreamReader(
resp.GetResponseStream()
).ReadToEnd()
).ToString()
);
}
 
// Send a PUT REST API request & print the HTTP response status
 
private static void DoPut(string account, string type, string query, StorageCredentials creds, string filePath)
{
HttpWebRequest req;
HttpWebResponse resp;
 
req = CreateRequest(account, type, "?restype=service&comp=properties", "PUT", creds, filePath);
 
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
Console.WriteLine(e.Message);
return;
}
 
Console.WriteLine("Response: " + resp.StatusCode);
}
 
// Create the REST API request
 
private static HttpWebRequest CreateRequest(string account, string type, string query, string method, StorageCredentials creds, string configFile)
{
// The URI includes the account name & service type ("blob" or "table")
Uri uri = new Uri(String.Format("http://{0}.{1}.core.windows.net/" + query, account, type));
var timeout = new TimeSpan(0, 0, 30);
StreamReader fileStream = null;
 
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
 
req.Timeout = (int)timeout.TotalMilliseconds;
req.ReadWriteTimeout = (int)timeout.TotalMilliseconds;
req.Method = method;
req.Headers["x-ms-version"] = "2009-09-19";
 
// Add the Content-Length
if (!String.IsNullOrEmpty(configFile))
{
fileStream = new StreamReader(configFile);
req.ContentLength = fileStream.BaseStream.Length;
}
 
// Each storage type requires a different signing scheme
if (type == "blob")
creds.SignRequest(req);
else
creds.SignRequestLite(req);
 
// Add the contents of the config file as the request body
// Must be done AFTER signing the request
if (fileStream != null)
{
fileStream.BaseStream.CopyTo(req.GetRequestStream());
fileStream.Close();
}
 
return req;
}
 
// Print command-line options
 
private static void Usage(OptionSet p, string s)
{
Console.Write("StorageAnalytics: ");
Console.WriteLine(s);
p.WriteOptionDescriptions(Console.Out);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.