Jun 292011
 

RackSpace enables setting TTL of container trough control panel. This setting sets the value of expire headers to all new files in container.
If TTL is set to 72h then expire header of file will be set to: current time + 72h.

Sadly you can’t set  TTL to more than 72 hours trough RackSpace web Control Panel. Cyberduck (free software for managing files in CDN) doesn’t support setting this too.

So the only way to set TTL of the container is trough API. I went trough C# examples on their website but I found them to be too  complex and confusing so I wrote my own code in C#.

It’s really simple. All you have to do is write one GET HTTP request to get the authentication token and one POST HTTP request to set the TTL.

Below is the source code of C# console application, all you have to do is to change the values of Username, APIKey and Container strings.

After you run the example the TTL of Container will be set to 1 year and far future expire headers of new files will have date one year ahead.

using System;
using System.Net;

class RackSpaceSetTTL
{
    static void Main(string[] args)
    {
        string Username = "yourusername"; //change to your username
        string APIKey = "yourapikey"; // change to your API Key
        string Container = "yourcontainer"; // change to your Container
        string TTLInSeconds = "31556926"; // 1 year
		
		//First http (GET) request: getting authentication token
        HttpWebRequest HttpRequest = (HttpWebRequest)
        WebRequest.Create("https://auth.api.rackspacecloud.com/v1.0");
        HttpRequest.Headers.Add("x-auth-user: " + Username);
        HttpRequest.Headers.Add("x-auth-key: " + APIKey);

        HttpWebResponse HttpResponse = (HttpWebResponse)
        HttpRequest.GetResponse();

        //string[] names1 = HttpResponse.Headers.AllKeys; //uncomment this to see all headers
        //foreach(string n in names1) Console.WriteLine("{0,-20}{1}", n, HttpResponse.Headers[n]); //uncomment this to see all headers

        string XAuthToken = HttpResponse.Headers["X-Auth-Token"].ToString();
        string XCDNManagementUrl = HttpResponse.Headers["X-CDN-Management-Url"].ToString();

        HttpResponse.Close();

        Console.WriteLine("X-Auth-Token: " + XAuthToken);
        Console.WriteLine("X-CDN-Management-Url: " + XCDNManagementUrl);
        Console.WriteLine("***");

		//Second http (POST) request: Setting TTL for container
        HttpRequest = (HttpWebRequest)
        WebRequest.Create(XCDNManagementUrl + "/" + Container);
        HttpRequest.Method = "POST";
        HttpRequest.Headers.Add("X-Auth-Token: " + XAuthToken);
        HttpRequest.Headers.Add("X-TTL: " + TTLInSeconds);
        HttpRequest.Headers.Add("X-CDN-Enabled: True");
        HttpRequest.Headers.Add("X-Log-Retention: False");

        HttpResponse = (HttpWebResponse)
        HttpRequest.GetResponse();

        //string[] names2 = HttpResponse.Headers.AllKeys; //uncomment this to see all headers
        //foreach (string n in names2) Console.WriteLine("{0,-20}{1}", n, HttpResponse.Headers[n]); //uncomment this to see all headers

        Console.WriteLine(HttpResponse.StatusDescription);

        HttpResponse.Close(); 

        Console.ReadLine();
    }
}

You can download file with source code here: Download setTTL.zip


Sorry, the comment form is closed at this time.