· 7 years ago · Oct 25, 2018, 06:18 AM
1using Amazon;
2using Amazon.CloudFront.Model;
3
4var client = AWSClientFactory.CreateAmazonCloudFrontClient(accessKey, secretKey);
5client.CreateInvalidation(new CreateInvalidationRequest {
6 DistributionId = distributionID,
7 InvalidationBatch = new InvalidationBatch {
8 Paths = new Paths {
9 Quantity = arrayofpaths.Length,
10 Items = arrayofpaths.ToList()
11 },
12 CallerReference = DateTime.Now.Ticks.ToString()
13 }
14});
15
16public static void InvalidateContent(string distributionId, string fileName)
17 {
18 string httpDate = Helpers.GetHttpDate();
19
20 ASCIIEncoding encoding = new ASCIIEncoding();
21 string postData = @"<InvalidationBatch>" +
22 " <Path>/" + fileName + "</Path>" +
23 " <CallerReference>" + httpDate + "</CallerReference>" +
24 "</InvalidationBatch>";
25 byte[] data = encoding.GetBytes(postData);
26
27 // Prepare web request...
28 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://cloudfront.amazonaws.com/2010-08-01/distribution/" + distributionId + "/invalidation");
29 webRequest.Method = "POST";
30 webRequest.ContentType = "text/xml";
31 webRequest.Headers.Add("x-amz-date", httpDate);
32
33 Encoding ae = new UTF8Encoding();
34 HMACSHA1 signature = new HMACSHA1(ae.GetBytes(GlobalSettings.AWSSecretAccessKey.ToCharArray()));
35 string b64 = Convert.ToBase64String(signature.ComputeHash(ae.GetBytes(webRequest.Headers["x-amz-date"].ToCharArray())));
36 webRequest.Headers.Add(HttpRequestHeader.Authorization, "AWS" + " " + GlobalSettings.AWSAccessKeyId + ":" + b64);
37
38 webRequest.ContentLength = data.Length;
39
40 Stream newStream = webRequest.GetRequestStream();
41 // Send the data.
42 newStream.Write(data, 0, data.Length);
43 newStream.Close();
44 }
45
46 /// <summary>
47 /// Gets a proper HTTP date
48 /// </summary>
49 public static string GetHttpDate()
50 {
51 // Setting the Culture will ensure we get a proper HTTP Date.
52 string date = System.DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss ", System.Globalization.CultureInfo.InvariantCulture) + "GMT";
53 return date;
54 }
55
56from datetime import datetime
57import urllib2, base64, hmac, hashlib
58
59def getHTTPDate():
60 return datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S UTC")
61
62def submitInvalidationRequest(fileName,distributionId):
63 url = "https://cloudfront.amazonaws.com/2010-08-01/distribution/" + distributionId + "/invalidation"
64 httpDate = getHTTPDate();
65 postData = "<InvalidationBatch>" +"<Path>/" + fileName + "</Path>" +"<CallerReference>" + httpDate + "</CallerReference>" +"</InvalidationBatch>";
66 sig = hmac.new(AWSSecretAccessKey, unicode(httpDate), hashlib.sha1)
67
68 headers = {"ContentType": "text/xml",
69 "x-amz-date": httpDate,
70 "Authorization":"AWS " + AWSAccessKeyId + ":" + base64.b64encode( sig.digest() )}
71
72 req = urllib2.Request(url,postData,headers)
73 return urllib2.urlopen(req).read()
74
75using Amazon.CloudFront.Model;
76
77var client = Amazon.AWSClientFactory.CreateAmazonCloudFrontClient(ConfigurationManager.AppSettings["Aws.AccessKey"],
78 ConfigurationManager.AppSettings["Aws.SecretKey"]);
79var request = new PostInvalidationRequest();
80request.DistributionId = ConfigurationManager.AppSettings["Cdn.DistributionId"];
81request.InvalidationBatch = new InvalidationBatch();
82request.InvalidationBatch.CallerReference = new Guid().ToString();
83request.InvalidationBatch.Paths = PathsInput.Text.Split(new[]{'n','r'},StringSplitOptions.RemoveEmptyEntries).ToList();
84var response = client.PostInvalidation(request);
85
86use warnings;
87use strict;
88use HTTP::Date;
89use Digest::SHA qw(hmac_sha1);
90use LWP::UserAgent;
91use MIME::Base64;
92use Encode qw(encode_utf8);
93
94@ARGV == 4 || die "usage: $0 url distribution_id accesskey secretkeyn";
95
96my $invalid_url = $ARGV[0];
97my $distribution_id = $ARGV[1];
98my $accesskey = $ARGV[2];
99my $secretkey = $ARGV[3];
100
101my $url = "https://cloudfront.amazonaws.com/2010-11-01/distribution/$distribution_id/invalidation";
102my $date = time2str;
103
104my $post_data = <<HERE;
105<?xml version="1.0" encoding="UTF-8"?>
106<InvalidationBatch>
107 <Path>$invalid_url</Path>
108 <CallerReference>$date</CallerReference>
109</InvalidationBatch>
110HERE
111
112my $sig = encode_base64(hmac_sha1(encode_utf8($date),encode_utf8($secretkey)));
113
114my $browser = LWP::UserAgent->new;
115my $res = $browser->post($url,
116 "Content" => $post_data,
117 "ContentType" => "text/xml",
118 "x-amz-date" => $date,
119 "Authorization" => "AWS $accesskey:$sig");
120
121print $res->status_line, "n", $res->content;
122
123ASCIIEncoding encoding = new ASCIIEncoding();
124 string postData = @"<InvalidationBatch>" +
125 " <Path>/" + fileName + "</Path>" +
126 " <CallerReference>" + httpDate + "</CallerReference>" +
127 "</InvalidationBatch>";
128 byte[] data = encoding.GetBytes(postData);
129
130 // Prepare web request...
131 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://cloudfront.amazonaws.com/2010-08-01/distribution/" + distributionId + "/invalidation");
132 webRequest.Method = "POST";
133 webRequest.ContentType = "text/xml";
134 webRequest.Headers.Add("x-amz-date", httpDate);
135
136 Encoding ae = new UTF8Encoding();
137 HMACSHA1 signature = new HMACSHA1(ae.GetBytes(GlobalSettings.AWSSecretAccessKey.ToCharArray()));
138 string b64 = Convert.ToBase64String(signature.ComputeHash(ae.GetBytes(webRequest.Headers["x-amz-date"].ToCharArray())));
139 webRequest.Headers.Add(HttpRequestHeader.Authorization, "AWS" + " " + GlobalSettings.AWSAccessKeyId + ":" + b64);
140
141 webRequest.ContentLength = data.Length;
142
143 Stream newStream = webRequest.GetRequestStream();
144 // Send the data.
145 newStream.Write(data, 0, data.Length);
146 newStream.Close();
147}