Akamai CDN清除站点中的所有文件

时间:2017-02-27 07:46:20

标签: javascript jquery asp.net akamai

我需要清除我的网站中使用AKAMAI CDN的所有图片。 我已经编写了清除一个文件的代码。 PFB单文件清除的代码。 但我的要求是清除网站中的所有文件。 任何人都可以建议如何实现这一目标。

由于

  

//   //©2016 WorldVentures。版权所有。   //

using Akamai.EdgeGrid.Auth;
using Akamai.Utils;
using CMS;
using CMS.DataEngine;
using CMS.DocumentEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

// Registers the custom module into the system
[assembly: RegisterModule(typeof(AkamaiPurge))]

public class AkamaiPurge : Module
{
    // Module class constructor, the system registers the module under the name "CustomInit"
    public AkamaiPurge()
        : base("CustomInit")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();

        // Assigns custom handlers to events
        DocumentEvents.Insert.After += Document_Insert_After;
        DocumentEvents.Update.After += Document_Update_After;
        DocumentEvents.Delete.After += Document_Delete_After;
    }

    /// <summary>
    /// Will be triggered when any document is added
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Document_Insert_After(object sender, DocumentEventArgs e)
    {
       // PurgeCache();
        PurgeCacheAkamai();
    }
    /// <summary>
    /// Will be triggered when any document is Updated
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Document_Update_After(object sender, DocumentEventArgs e)
    {
        //PurgeCache();
        PurgeCacheAkamai();
    }

    /// <summary>
    /// Will be triggered when any document is Deleted
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Document_Delete_After(object sender, DocumentEventArgs e)
    {
        //PurgeCache();
        PurgeCacheAkamai();
    }

    /// <summary>
    /// Following function will invalidate cache from Akamai server
    /// </summary>
    public void PurgeCache()
    {

        string secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string clientToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string accessToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string apiurl = "https://akab-7t5mh54r7lq3a7d7-js5q6mdtx42qimcn.purge.akamaiapis.net/ccu/v3/invalidate/url/production";

        List<string> headers = new List<string>();
        string httpMethod = "POST";
        string data = "{\"hostname\": \"www.qa.worldventures.com\",\"objects\": [\"www.qa.worldventures.com/getmedia/9931b92a-c7f3-4a71-ab27-37e2b13572c0/should-be-here.jpg?width=2541&height=1811&ext=.jpg\"]}";

        Stream uploadStream = null;
        uploadStream = new MemoryStream(data.ToByteArray());

        var uri = new Uri(apiurl);
        var request = WebRequest.Create(uri);

        request.Method = httpMethod;
        var signer = new EdgeGridV1Signer();
        var credential = new ClientCredential(clientToken, accessToken, secret);

        signer.Sign(request, credential);

        using (var result = signer.Execute(request, credential))
        {
            using (result)
            {
                using (var reader = new StreamReader(result))
                {
                    string value = reader.ReadToEnd();
                }
            }
        }
    }

    /// <summary>
    /// Following function will invalidate cache from Akamai server
    /// </summary>
    public void PurgeCacheAkamai()
    {
        string secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string clientToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string accessToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string apiurl = "https://akab-7t5mh54r7lq3a7d7-js5q6mdtx42qimcn.purge.akamaiapis.net/ccu/v3/invalidate/url/production";
        List<string> headers = new List<string>();
        string httpMethod = "POST";
        string outputfile = null;
        string data = "{\"hostname\": \"www.qa.worldventures.com\",\"objects\": [\"www.qa.worldventures.com/getmedia/9931b92a-c7f3-4a71-ab27-37e2b13572c0/should-be-here.jpg?width=2541&height=1811&ext=.jpg\"]}";
        int maxBodySize = 2048;

        EdgeGridV1Signer signer = new EdgeGridV1Signer(null, maxBodySize);
        ClientCredential credential = new ClientCredential(clientToken, accessToken, secret);

        Stream uploadStream = null;
        uploadStream = new MemoryStream(data.ToByteArray());

        var uri = new Uri(apiurl);
        var request = WebRequest.Create(uri);

        foreach (string header in headers) request.Headers.Add(header);
        request.Method = httpMethod;

        Stream output = Console.OpenStandardOutput();
        if (outputfile != null)
            output = new FileInfo(outputfile).OpenWrite();

        signer.Sign(request, credential, uploadStream);

        using (var result = signer.Execute(request, credential, uploadStream))
        {
            using (output)
            {
                using (result)
                {
                    using (var reader = new StreamReader(result))
                    {
                        string value = reader.ReadToEnd();
                    }
                }
            }
        }

        //using (var result = signer.Execute(request, credential))
        //{
        //    using (result)
        //    {
        //        using (var reader = new StreamReader(result))
        //        {
        //            string value = reader.ReadToEnd();
        //        }
        //    }
        //}
    }
    public void PurgeCacheAkamai2()
    {
        // Create a request using a URL that can receive a post.   
        WebRequest request = WebRequest.Create("https://akab-7t5mh54r7lq3a7d7-js5q6mdtx42qimcn.purge.akamaiapis.net/ccu/v3/invalidate/url/production");
        // Set the Method property of the request to POST.  
        request.Method = "POST";
        // Create POST data and convert it to a byte array.  
        string postData = "This is a test that posts this string to a Web server.";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.  
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.  
        request.ContentLength = byteArray.Length;
        // Get the request stream.  
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.  
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.  
        dataStream.Close();
        // Get the response.  
        WebResponse response = request.GetResponse();
        // Display the status.  
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.  
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.  
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.  
        string responseFromServer = reader.ReadToEnd();
        // Display the content.  
        Console.WriteLine(responseFromServer);
        // Clean up the streams.  
        reader.Close();
        dataStream.Close();
        response.Close();
    }
}

1 个答案:

答案 0 :(得分:0)

这取决于你想怎么做。

例如,如果您仍在使用akamai的CCU或快速清除功能。

如果您使用的是CCU或快速清除,那么最好的办法是将所有图像标记为专用的CP代码。 完成后,您可以清除CP代码,该代码将实习清除所有图像。

您可以参考CCU的几个有用文件: https://developer.akamai.com/api/purge/ccu-v2/overview.html

快速清除: https://control.akamai.com/dl/customers/FIMA/Fast-Purge-QuickRef.pdf https://github.com/akamai-open/api-kickstart/tree/master/examples

感谢, 维诺德