Custom AWS S3 Helper Class AWSSDK Wrapper
Implements some very commonly used AWS S3 functionality. (need to merge with my other AWS wrapper classes, Route53, EC2, etc)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using System.Collections.Specialized; using System.Configuration; //uses AWSSDK.dll from amazon using Amazon; using Amazon.S3; using Amazon.S3.Model; using System.Xml.Linq; using System.Xml; using System.Data; namespace AIS.Common { public static class AWSHelper { private static List<S3Bucket> LoadS3Buckets() { System.Collections.Specialized.NameValueCollection appConfig = System.Configuration.ConfigurationManager.AppSettings; using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey)) { return s3client.ListBuckets().Buckets; } } private static List<S3Object> LoadS3Objects(string bucketname) { System.Collections.Specialized.NameValueCollection appConfig = System.Configuration.ConfigurationManager.AppSettings; using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey)) { return s3client.ListObjects(new ListObjectsRequest() { BucketName = bucketname }).S3Objects; } } private static void LoadS3File(string bucketname, string keyname, HttpResponse response, string contenttype) { NameValueCollection appConfig = ConfigurationManager.AppSettings; using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey)) { GetObjectRequest s3request = new GetObjectRequest() .WithBucketName(bucketname).WithKey(keyname); using (GetObjectResponse s3response = s3client.GetObject(s3request)) { string title = s3response.Metadata["x-amz-meta-title"]; response.Clear(); //Response.Write(string.Format("The object's title is {0}", title)); //Response.AddHeader //Response.ContentType="application/swf"; ////Response.ContentType="contenttype"; response.ContentType = s3response.ContentType; //s3response.Headers["Content-Length"]; long filesize = s3response.ContentLength; byte[] buffer = new byte[(int)filesize]; response.BinaryWrite(ConvertStreamToBytes(s3response.ResponseStream, filesize)); response.Flush(); response.Close(); } } } public static string GetS3UrlToVideo(string bucketname, string keyname) { System.Collections.Specialized.NameValueCollection appConfig = System.Configuration.ConfigurationManager.AppSettings; string url = ""; using (var s3client = Amazon.AWSClientFactory.CreateAmazonS3Client(ConfigValues.AWSAccessKey, ConfigValues.AWSSecretKey)) { Amazon.S3.Model.GetPreSignedUrlRequest request = new Amazon.S3.Model.GetPreSignedUrlRequest() .WithBucketName(bucketname) .WithKey(keyname) .WithProtocol(Amazon.S3.Model.Protocol.HTTP) .WithVerb(HttpVerb.GET) .WithExpires(DateTime.Now.AddMinutes(ConfigValues.VideoURLExpiration)); Amazon.S3.Model.GetPreSignedUrlRequest r = new GetPreSignedUrlRequest(); url = s3client.GetPreSignedURL(request); url= "https://s3.amazonaws.com/" + bucketname + keyname; } //return System.Xml.XmlConvert.EncodeName(url); return url; } public static byte[] ConvertStreamToBytes(Stream input, long filesize) { byte[] buffer = new byte[(int)filesize]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } } } }
Custom Web.Config Wrapper Class ASP .NET
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; namespace AIS.Common { public class ConfigValues { #region appSettings public static string SomeStringOne { get { return getval("SomeStringOne "); } } public static string SomeStringTwo { get { return getval("SomeStringTwo "); } } public static string Env { get { return getval("env"); } } //keep in mind case sensitivity public static string LastManualRefresh { get { return getval("date_last_manual_refresh"); } } //useful for manual site refresh/reload public static double SomeDouble { get { return Convert.ToDouble(getval("some_static_double")); } } #endregion #region connectionStrings - update web.config env variable to toggle between dev and prd public static string YourDBOneConnectionString { get { return getcstr("win_web_db"); } } //also read only implementation like above, but illustrates environment variable usage specific in web.config useful if you have many environments public static string YourDBTwoConnectionString { get { if (Env.ToLower().ToString() != "filesystem") { return getcstr("static_string" + Env.ToLower().ToString()); } return ""; } } #endregion /// <summary> /// Retrieve Connection String for specified provided key /// </summary> /// <param name="key"></param> /// <returns></returns> private static string getcstr(string key) { try { return ConfigurationManager.ConnectionStrings[key].ConnectionString; } catch (Exception ex) { Shared.HandleError(ex); //TODO: change to throw error event handle instead of direct call for reusability return "Error retrieving value"; } } /// <summary> /// Retrieve appSettings value for provided specified key /// </summary> /// <param name="key"></param> /// <returns></returns> private static string getval(string key) { try { return ConfigurationManager.AppSettings[key]; } catch (Exception ex) { Shared.HandleError(ex); //TODO: change to throw error event handle instead of direct call for reusability return "Error retrieving value"; } } } }
Custom URLRewriting with XML and Global.asax
utilizes my custom DocParser class here https://ronniediaz.com/2013/08/23/parse-xml-to-dynamic-expandoobject-c-net-4-0/
void Application_BeginRequest(object sender, EventArgs e) { //string path = Request.Url.ToString(); string path = Request.Path; if (Regex.IsMatch(path.ToLower(), ".*[.](jpg|png|gif|css|js|pdf|ico|woff|svg|eot|ttf)$")) { //var h = Regex.Replace(path.ToLower(), ".*/(.*)/(.*)$", "~/$1/$2", RegexOptions.Singleline); //Context.RewritePath(path); return; } //TODO: authorize user/request DocParser dp = new DocParser(Server.MapPath("~/rewrite.xml")); var rules = dp.GetElements("*/rule"); foreach (var r in rules) { if (string.IsNullOrEmpty(r.match_url)) { Shared.HandleError("Global.asax::null or empty match_url encountered"); } if (string.IsNullOrEmpty(r.action_url)) { Shared.HandleError("Global.asax::null or empty action_url encountered"); } if (Regex.IsMatch(path, r.match_url,RegexOptions.IgnoreCase)) { List<object> qsvars = new List<object>(); foreach (Match m in Regex.Matches(path, r.match_url, RegexOptions.IgnoreCase)) { for (int i=1;i<m.Groups.Count;i++) { qsvars.Add(m.Groups[i].Value); } } //Context.RewritePath(string.Format(r.action_url,qsvars.ToArray()) + "&rewrite=1"); Context.RewritePath(string.Format(r.action_url, qsvars.ToArray())); } } }
rewrite.xml examples
<?xml version="1.0" encoding="utf-8" ?> <rules> <rule match_url="^/home" action_url="~/home.aspx" /> <rule match_url="^/login" action_url="~/default.aspx" /> <rule match_url="^/register" action_url="~/welcome/register.aspx" /> <rule match_url="^/logout" action_url="~/logout.aspx" /> <rule match_url="^/default/(.*)" action_url="~/default.aspx?q={0}" /> <rule match_url="^/test/(.*)/(.*)" action_url="~/test.aspx?q={0}&r={1}" /> </rules>
Common functions asp .net static shared library
references custom encryption class here https://ronniediaz.com/2011/01/13/quick-net-encryption-reference/
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.Security; using System.Text; using System.Net; using System.IO; namespace AIS.Common { public static class Shared { //TODO: add local, dev, prd public static string basehref { get { return HttpContext.Current.Request.ApplicationPath; } } //TODO: change to json serialized object reference to prevent manipulation of pipe delimeters public static string SetSecureQueryString(object o) { string ct = Crypto.Rijndael.Encrypt(o.ToString(), "static_or_dynamic_string"); return HttpUtility.UrlEncode(ct); } //TODO: change to json (same as above) public static string SetSecureQueryString(object a, object b, object c, object d, object e, object f, object g, object h) { string ct = Crypto.Rijndael.Encrypt(a.ToString() + "|" + b.ToString() + "|" + c.ToString() + "|" + d.ToString() + "|" + e.ToString() + "|" + f.ToString() + "|" + g.ToString() + "|" + h.ToString(), "livestream"); return HttpUtility.UrlEncode(ct); } public static string GetSecureQueryString(string q) { try { //automatically decodes querystring in request return Crypto.Rijndael.Decrypt(q, "static_or_dynamic_string"); } catch (Exception ex) { return ""; } } //retrieves content from web url and returns as string public static string DownloadString(string url, NetworkCredential nc=null) { System.Net.WebClient wc = new WebClient(); if (nc != null) { wc.UseDefaultCredentials = true; wc.Credentials = nc; } return wc.DownloadString(url); } //http://mayur.gondaliya.com/programming-languages/c-sharp/samples/fetching-url-contents-into-a-string-using-httpwebrequest-72.html //same as above function, different approach with more detail public static string DownloadPage(string url) { const int bufSizeMax = 65536; // max read buffer size conserves memory const int bufSizeMin = 8192; // min size prevents numerous small reads StringBuilder sb; // A WebException is thrown if HTTP request fails try { // Create an HttpWebRequest using WebRequest.Create (see .NET docs)! HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = WebRequestMethods.Http.Get; // Execute the request and obtain the response stream HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); // Content-Length header is not trustable, but makes a good hint. // Responses longer than int size will throw an exception here! int length = (int)response.ContentLength; // Use Content-Length if between bufSizeMax and bufSizeMin int bufSize = bufSizeMin; if (length > bufSize) bufSize = length > bufSizeMax ? bufSizeMax : length; // Allocate buffer and StringBuilder for reading response byte[] buf = new byte[bufSize]; sb = new StringBuilder(bufSize); // Read response stream until end while ((length = responseStream.Read(buf, 0, buf.Length)) != 0) sb.Append(Encoding.UTF8.GetString(buf, 0, length)); return sb.ToString(); } catch (Exception ex) { sb = new StringBuilder(ex.Message); return sb.ToString(); } } //one approach using filesystem. will only work depending on permissions otherwise url parsing is needed public static string GetCurrentPageName() { string spath = System.Web.HttpContext.Current.Request.Url.AbsolutePath; System.IO.FileInfo oInfo = new System.IO.FileInfo(spath); string spagename = oInfo.Name; return spagename; } //get titlecase string public static string GetTitleCase(object str) { try { return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToString().ToLower()); } catch (Exception ex) { Shared.HandleError(ex); return "Other"; } } #region handleerror public static void HandleError(Exception ex) { HandleError(ex.ToString()); } //TODO: send error to error handler public static void HandleError(string details) { SendEmail(details); } public static void HandleError(Page p, Exception ex) { HandleError(p, ex.ToString()); } public static void HandleError(Page p, string details) { //MessageBox.Show(hwnd, details); SendEmail(details); } //TODO: replace with call to error manager or hook to event for compatibility public static void SendEmail(string details) { //throw new HttpUnhandledException(details); if (Shared.GetCurrentPageName().Replace("/", "") != "error.aspx") //TODO: remove. temporary resolution for unhandled session timeouts. PROTOTYPE { //HttpContext.Current.Response.Redirect("~/error.aspx"); //TODO: global error log and local } } #endregion } }
Could not load type System.ServiceModel.Activation.HttpModule’ from assembly ‘System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=
If you already have .Net 4.0 installed and install a program that brings in an earlier version of .Net, or manually install an earlier version of .Net afterwards, you may get an exception page that matches the title of this article.
This is a simple fix, outlined in detail in MSDN article referenced at the bottom.
Navigate to:
%windir%\Microsoft.NET\Framework\v4.0.30319 %windir%\Microsoft.NET\Framework64\v4.0.30319 (on a 64-bit computer)
Run:
aspnet_regiis.exe /iru
That’s it!
ubuntu lts 12 xrdp rdp setup on Amazon Cloud AWS(12.04.3 precise pangolin)
Consolidated from multiple sources to exclude erroneous steps.
Tested on Amazon Web Services fresh Ubuntu LTS 12.04.3 instance.
Decided upon ubuntu vs centOS simply to avoid having to compile xrdp and manage dependencies manually. Chose 3rd party linux over Amazon AMI in this case for benefit of included repos, use cases and support in their respective communities (ubuntuforums, etc), as well as not having to compile xrdp in Amazon AMI.
sudo useradd -m {name} passwd {name} #you may want to consider setting a root passwd in case you mess up sudoers. if you make a mistake with sudoers terminate your instance and relaunch 🙂 ... or you could mount the HD with another system if it's unencrypted and modify sudoers passwd root #edit /etc/sudoers.d and add your new user {name} ALL=(ALL) ALL #edit sshd /etc/ssh/sshd_config disable root login and allow password authentication (if you like) service ssh restart #http://www.liberiangeek.net/2012/05/connect-to-ubuntu-12-04-precise-pangolin-via-windows-remote-desktop/ sudo apt-get install xrdp sudo apt-get install gnome-session-fallback #two options, 2d unity no longer available, previously echo "gnome-session --session=ubuntu-2d" > ~/.xsession #http://askubuntu.com/questions/247501/i-get-failed-to-load-session-ubuntu-2d-when-using-xrdp echo gnome-session --session=gnome-fallback > ~/.xsession #make sure your user has permission to this file if you have to create it with sudo etc chown {name}:{name} .xsession #http://www.filiwiese.com/installing-gnome-on-ubuntu-12-04-precise-pangolin/ sudo add-apt-repository ppa:gnome3-team/gnome3 sudo apt-get update sudo apt-get install gnome-shell #http://askubuntu.com/questions/251041/how-to-install-lightdm-set-defaults sudo apt-get install lightdm #http://askubuntu.com/questions/71126/how-do-i-set-the-gnome-classic-login-to-be-the-default-with-autologin sudo /usr/lib/lightdm/lightdm-set-defaults -s gnome-fallback
Don’t forget AWS Firewall:
22 (SSH) your ip/32
Amazon WorkSpaces
https://aws.amazon.com/workspaces/
This service may highlight the beginning of a revolution in how we do personal and professional computing.
MSSQL User Defined Functions vs Stored Procedures
I received this question earlier today, and thought it was a valid question often misunderstood, and deserving of a small write-up:
“Should a User Defined Function be your first choice instead of a Stored Procedure?”
While there are many pros and cons of each not covered in this write-up (review your versions on MSDN for details), including some features which may not be apparent until you have an issue to troubleshoot (such as sp_who filtering), you can generally ask yourself a single question up front that can help you determine which you should use.
Simply, if the db functionality you need to implement in the function/procedure requires
any DML (insert/update/delete), then go with a stored procedure. Advanced selects and/or filters are best left up to views/table valued functions.
Additionally, do not be afraid to use a combination of functions and procedures especially if there is a goal of re-usability, in accordance with the design considering the planned growth of your db as your software & db architecture permits. On that note, consider and test the performance differences of these implementations, as a compiled/cached function/procedure containing more logic internally may outperform one utilizing logic that is spread throughout.
References
SP_who filtering UDF vs SP, http://stackoverflow.com/questions/2567141/use-sql-to-filter-the-results-of-a-stored-procedure
Data Manipulation Language, http://en.wikipedia.org/wiki/Data_manipulation_language
(Some) Differences (about.com), http://databases.about.com/od/sqlserver/a/procs_vs_functs.htm
(Some) Differences (stackoverflow), http://stackoverflow.com/questions/2039936/difference-between-stored-procedures-and-user-defined-functions