Skip to content

Instantly share code, notes, and snippets.

@jlyonsmith
Created February 11, 2014 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlyonsmith/8929291 to your computer and use it in GitHub Desktop.
Save jlyonsmith/8929291 to your computer and use it in GitHub Desktop.
S3Helper
using System;
using System.Net;
using System.Net.Http;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using ToolBelt;
using System.IO;
//
// Code that did the pre-v4 Authorization for AWS. Superceded by the vastly more complicated AWS-v4 algorithm.
//
namespace Artifact.Utility
{
public static class S3Region
{
public static readonly string USStandard = "s3-external-1.amazonaws.com";
public static readonly string USWestOregon = "s3-us-west-2.amazonaws.com";
public static readonly string USWestCalifornia = "s3-us-west-1.amazonaws.com";
}
public class S3Helper
{
public S3Helper(string s3AccessKeyId, string s3SecretAccessKey, string s3Region)
{
S3SecretAccessKey = s3SecretAccessKey;
S3AccessKeyId = s3AccessKeyId;
S3Region = s3Region;
}
public string S3AccessKeyId { get; set; }
public string S3SecretAccessKey { get; set; }
public string S3Region { get; set; }
public HttpWebRequest CreateS3Request(
HttpMethod method,
string bucketName = null,
string objectName = null,
IEnumerable<KeyValuePair<string, string>> amzHeaders = null)
{
// See http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html for the gory details of this call
string host = "s3.amazonaws.com";
string url = "http://";
string resource = "/";
if (bucketName != null)
{
host = bucketName + "." + host;
if (objectName != null)
{
if (objectName.StartsWith("/"))
objectName = objectName.Substring(1);
resource += objectName;
}
}
url += host + resource;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
WebHeaderCollection headers = (request as HttpWebRequest).Headers;
request.Method = method.ToString();
request.Host = host;
request.Date = DateTime.UtcNow;
string contentMD5 = "";
string contentType = "";
string amzHeaderString;
if (amzHeaders != null && amzHeaders.Count() > 0)
{
foreach (var amzHeader in amzHeaders)
{
headers.Add(amzHeader.Key, amzHeader.Value);
}
amzHeaderString = StringUtility.Join("\n", amzHeaders.Distinct().OrderBy(h => h.Key)
.Select(h => String.Format("{0}:{1}", h.Key.ToLower(), h.Value.Replace("\n", " "))).ToList()) + "\n";
}
else
amzHeaderString = "";
string canonicalString = String.Format(
"{0}\n{1}\n{2}\n{3}\n{4}{5}",
method.ToString(),
contentMD5,
contentType,
headers["Date"],
amzHeaderString,
resource);
HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(S3SecretAccessKey));
headers.Add("Authorization", "AWS " + S3AccessKeyId + ":" + Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(canonicalString))));
return request;
}
public static string GetResponseString(WebResponse response)
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(responseStream))
{
return sr.ReadToEnd();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment