Skip to content

Instantly share code, notes, and snippets.

@tanglebones
Last active December 17, 2015 04:19
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 tanglebones/5549945 to your computer and use it in GitHub Desktop.
Save tanglebones/5549945 to your computer and use it in GitHub Desktop.
Creating a signed S3 policy for a jpeg image.
var policy = new JObject();
policy["expiration"] = DateTime.UtcNow.AddMinutes(10).ToString("u").Replace(" ", "T");
var cond = new JArray();
policy["conditions"] = cond;
cond.Add(new JObject {{"bucket", Bucket}});
cond.Add(new JObject {{"key", filename}});
cond.Add(new JObject {{"acl", "public-read"}});
cond.Add(new JObject {{"Content-Type", "image/jpeg"}});
cond.Add(new JArray("content-length-range", 0, 1024*1024));
var policyMin = policy.ToString(Formatting.None).Replace("\r", "").Replace("\n", "");
var policyEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(policyMin));
var signature = ComputeSignature(policyEncoded, PrivateKey);
var resp = new JObject();
resp["key"] = filename;
resp["AWSAccessKeyId"] = PublicKey;
resp["acl"] = "public-read";
resp["policy"] = policyEncoded;
resp["policyMin"] = policyMin;
resp["Content-Type"] = "image/jpeg";
resp["signature"] = signature;
resp["caName"] = filename;
resp["s3post_url"] = "http://" + Bucket + ".s3.amazonaws.com/";
resp["public_url"] = "http://" + Bucket + ".s3.amazonaws.com/" + filename;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment