Skip to content

Instantly share code, notes, and snippets.

@chitsaou chitsaou/README.md
Last active Mar 29, 2018

Embed
What would you like to do?
Amazon S3 Redirect Rules Generator

Amazon S3 Redirect Rules Generator

A Ruby script to generate simple Amazon S3 Redirection Rules XML file.

Update: There is an app for that now! Use Amazon S3 Redirector (Web app) and you can generate the XML without any knowledge about Ruby. Thanks to @rainforestapp. BTW, It's open source too.

Dependencies

  • Nokogiri

Usage

ruby s3-routes-generator.rb input.txt

The generated XML is printed to STDOUT.

Assumptions on S3 Bucket

It assumes that:

  1. Website Hosting is enabled.
  2. Index Document is set to index.html.

Conversion Rules

  1. For target path, if it ends with /, then index.html will be appended.
  2. For source and target path, the leading / will be removed, because S3's object key does not begin with /.

Sample Input

One rule per line. Each rule is constructed by source and target path, spearated by at least 1 space.

Please not that rules are matched using KeyPrefixEquals, and is First-In-First-Out, so if you have multiple redirections from same prefixes (typically /dir and /dir/subdir), please put the longer one before the shorter one, i.e. Depth-First.

/home                        /
/products/iphone/specs.html  /iphone/specs.html
/products/iphone/            /iphone/
/products/ipad/accessories/  /ipad/accessories.html
/products/ipad/              /ipad/
/products                    /

Sample Output

<?xml version="1.0"?>
<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>home</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>products/iphone/specs.html</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>iphone/specs.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>products/iphone/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>iphone/index.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>products/ipad/accessories/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>ipad/accessories.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>products/ipad/</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>ipad/index.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals>products</KeyPrefixEquals>
    </Condition>
    <Redirect>
      <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
  </RoutingRule>
</RoutingRules>

References

License

None. I release this script to Public Domain.

require 'nokogiri'
routes = []
src = File.read(ARGV[0])
src.each_line do |line|
from, to = line.split(/\s+/)
from.gsub!(/\A\//, '')
to.gsub!(/\/\z/, '/index.html')
to.gsub!(/\A\//, '')
routes << { :from => from, :to => to }
end
builder = Nokogiri::XML::Builder.new do |xml|
xml.RoutingRules {
routes.each do |route|
xml.RoutingRule {
xml.Condition {
xml.KeyPrefixEquals route[:from]
}
xml.Redirect {
xml.ReplaceKeyWith route[:to]
}
}
end
}
end
puts builder.to_xml
@RameshJhajharia

This comment has been minimized.

Copy link

RameshJhajharia commented Nov 3, 2016

Is it possible to detect user agent from the request and redirect based on the user agent on amazon s3 static hosting?

@frob

This comment has been minimized.

Copy link

frob commented Jul 13, 2017

I am getting The XML you provided was not well-formed or did not validate against our published schema when I try to use this. It looks correct to me though.

@taqtiqa-mark

This comment has been minimized.

Copy link

taqtiqa-mark commented Jul 27, 2017

@frob the cause of the error message is the first line <?xml version="1.0"?>. Remove that and AWS accepts the XML. Having said that I am using handwritten XML but it looks the same to me - when I inserted the line I cited I saw that error message. HTH

@grantgeorge

This comment has been minimized.

Copy link

grantgeorge commented Jan 10, 2018

@RameshJhajharia I'm wondering the same thing. Did you ever arrive at an answer?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.