Skip to content

Instantly share code, notes, and snippets.

@yorkxin
Last active October 27, 2020 12:17
Show Gist options
  • Select an option

  • Save yorkxin/5319661 to your computer and use it in GitHub Desktop.

Select an option

Save yorkxin/5319661 to your computer and use it in GitHub Desktop.

Revisions

  1. Yu-Cheng Chuang revised this gist Nov 16, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

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

    **Update**: There is an app for that now! Use [Amazon S3 Redirector](http://quiet-cove-8872.herokuapp.com/) (Web app) and you can generate the XML without any knowledge about Ruby. Thanks to @rainforestapp. BTW, [It's open source too](https://github.com/rainforestapp/amazon-s3-redirects).
    **Update**: There is an app for that now! Use [Amazon S3 Redirector](http://quiet-cove-8872.herokuapp.com/) (Web app) and you can generate the XML without any knowledge about Ruby. Thanks to [@rainforestapp](https://github.com/rainforestapp/amazon-s3-redirects). BTW, [It's open source too](https://github.com/rainforestapp/amazon-s3-redirects).

    ## Dependencies

  2. Yu-Cheng Chuang revised this gist Nov 16, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    # Amazon S3 Redirect Rules Generator

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

    **Update**: There is an app for that now! Use [Amazon S3 Redirector](http://quiet-cove-8872.herokuapp.com/) (Web app) and you can generate the XML without any knowledge about Ruby. Thanks to @rainforestapp. BTW, [It's open source too](https://github.com/rainforestapp/amazon-s3-redirects).

    ## Dependencies

  3. yorkxin revised this gist Apr 5, 2013. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,13 @@ A Ruby script to generate simple Amazon S3 Redirection Rules.

    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.
    @@ -86,13 +93,6 @@ Please not that rules are matched using `KeyPrefixEquals`, and is First-In-First
    </RoutingRules>
    ```

    ## Assumptions

    It assumes that:

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

    ## References

    * [Configure a Bucket for Website Hosting - Amazon Simple Storage Service](http://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html#configure-bucket-as-website-routing-rule-syntax)
  4. yorkxin revised this gist Apr 5, 2013. 1 changed file with 41 additions and 5 deletions.
    46 changes: 41 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -12,11 +12,23 @@ A Ruby script to generate simple Amazon S3 Redirection Rules.

    The generated XML is printed to STDOUT.

    ## 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

    /home /
    /products/iphone /iphone
    /products /
    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

    @@ -33,10 +45,34 @@ The generated XML is printed to STDOUT.
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products/iphone</KeyPrefixEquals>
    <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>iphone</ReplaceKeyWith>
    <ReplaceKeyWith>ipad/index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
  5. yorkxin revised this gist Apr 5, 2013. 1 changed file with 29 additions and 27 deletions.
    56 changes: 29 additions & 27 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -20,33 +20,35 @@ The generated XML is printed to STDOUT.

    ## 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</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>iphone</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    </RoutingRules>
    ```xml
    <?xml version="1.0"?>
    <RoutingRules>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>home</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products/iphone</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>iphone</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    </RoutingRules>
    ```

    ## Assumptions

  6. yorkxin revised this gist Apr 5, 2013. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@

    A Ruby script to generate simple Amazon S3 Redirection Rules.

    ## Dependency
    ## Dependencies

    Nokogiri
    * Nokogiri

    ## Usage

    @@ -55,6 +55,10 @@ It assumes that:
    1. Website Hosting is enabled.
    2. Index Document is set to `index.html`.

    ## References

    * [Configure a Bucket for Website Hosting - Amazon Simple Storage Service](http://docs.aws.amazon.com/AmazonS3/latest/dev/HowDoIWebsiteConfiguration.html#configure-bucket-as-website-routing-rule-syntax)

    ## License

    None. I release this script to Public Domain.
  7. yorkxin revised this gist Apr 5, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Amazon S3 Redirect Rules Generator

    A Ruby script to generate simple Amazon S3 Redirection Rules.

    ## Dependency
  8. yorkxin revised this gist Apr 5, 2013. 1 changed file with 31 additions and 31 deletions.
    62 changes: 31 additions & 31 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,45 +6,45 @@ Nokogiri

    ## Usage

    ruby s3-routes-generator.rb input.txt
    ruby s3-routes-generator.rb input.txt

    The generated XML is printed to STDOUT.

    ## Sample Input

    /home /
    /products/iphone /iphone
    /products /
    /home /
    /products/iphone /iphone
    /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</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>iphone</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    </RoutingRules>
    <?xml version="1.0"?>
    <RoutingRules>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>home</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products/iphone</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>iphone</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    </RoutingRules>

    ## Assumptions

  9. yorkxin created this gist Apr 5, 2013.
    58 changes: 58 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    A Ruby script to generate simple Amazon S3 Redirection Rules.

    ## Dependency

    Nokogiri

    ## Usage

    ruby s3-routes-generator.rb input.txt

    The generated XML is printed to STDOUT.

    ## Sample Input

    /home /
    /products/iphone /iphone
    /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</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>iphone</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    <RoutingRule>
    <Condition>
    <KeyPrefixEquals>products</KeyPrefixEquals>
    </Condition>
    <Redirect>
    <ReplaceKeyWith>index.html</ReplaceKeyWith>
    </Redirect>
    </RoutingRule>
    </RoutingRules>

    ## Assumptions

    It assumes that:

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

    ## License

    None. I release this script to Public Domain.
    33 changes: 33 additions & 0 deletions s3-routes-generator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    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