Last active
December 17, 2015 04:19
-
-
Save tanglebones/5549945 to your computer and use it in GitHub Desktop.
Creating a signed S3 policy for a jpeg image.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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