Attached is a short script I hacked together from details at
http://tomatohater.com/2010/10/22/cloudfront-object-invalidation-python/

I'd love to see that functionality in s3cmd, and I am working to learn
more about s3cmd in hopes of providing a patch to do that.  In the
mean time if anyone is looking for a quick way to invalidate a file(s)
you can use the attached.

-Jim P.
#!/usr/bin/python
##

import urllib2
import base64
import datetime
import hmac
import hashlib
from xml.dom import minidom
import sys

AWS_CF_DISTRIBUTION_ID = "<your_dist_id>"
AWS_ACCESS_KEY = "<your_access_key>"
AWS_SECRET_ACCESS_KEY = "<your_secret_key>"

host = 'cloudfront.amazonaws.com'
path = '/2010-11-01/distribution/%s/invalidation' % AWS_CF_DISTRIBUTION_ID

def get_aws_auth():
    date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
    auth = 'AWS %s:%s' % (
        AWS_ACCESS_KEY,
        base64.b64encode(
            hmac.new(
                AWS_SECRET_ACCESS_KEY.encode('utf-8'),
                date.encode('utf-8'),
                hashlib.sha1
            ).digest()
        )
    )
    return date, auth


if len(sys.argv) < 2:
    date, auth = get_aws_auth()

    # define required http headers
    headers = {
        'Host': host,
        'Authorization' : auth,
        'Date': date,
    }

    # send REST request and print results
    request_url = 'https://%s%s' % (host, path)
    req = urllib2.Request(request_url, headers=headers)
    response = urllib2.urlopen(req)
    xml = response.read()
    print minidom.parseString(xml).toprettyxml(indent=' '*4)
    sys.exit()


# create authentication signature
date, auth = get_aws_auth()

# create xml data to post
data = ['<?xml version="1.0" encoding="UTF-8"?>']
data.append('<InvalidationBatch>') 
data.append('%s' % ''.join(['<Path>/%s</Path>' % f for f in sys.argv[1:]]))
data.append('<CallerReference>%s</CallerReference>' % date)
data.append('</InvalidationBatch>')
data = ''.join(data)

# define required http headers
headers = {
        'Host': host,
        'Authorization' : auth,
        'Date': date,
        'Content-Type': 'text/xml',
        'Content-Length': len(data),
    }

# make REST request, capture 201 (success) response
request_url = 'https://%s%s' % (host, path)
req = urllib2.Request(request_url, data, headers=headers)
try:
    response = urllib2.urlopen(req)
except urllib2.HTTPError, e:
    print e
    if not e.code == 201:
        print "---------------------------------"
        print "URL:", request_url
        print ""
        print "Headers:"
        print minidom.parseString(headers).toprettyxml(indent=' '*4)
        print ""
        print "Data:"
        print minidom.parseString(data).toprettyxml(indent=' '*4)
        print "---------------------------------"

------------------------------------------------------------------------------
Gaining the trust of online customers is vital for the success of any company
that requires sensitive data to be transmitted over the Web.   Learn how to 
best implement a security strategy that keeps consumers' information secure 
and instills the confidence they need to proceed with transactions.
http://p.sf.net/sfu/oracle-sfdevnl 
_______________________________________________
S3tools-general mailing list
S3tools-general@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/s3tools-general

Reply via email to