image

Sascha Depold

Engineering Manager

Blog

Duplicating a bucket on Amazon S3 with Ruby

Due to my studies I’m currently working with the Amazon Services Elastic Compute Cloud (EC2) and the Simple Storage Service (S3).

I wanted to easyly duplicate a bucket on S3 but found no method to do so. So I just wrote a script in Ruby which just makes a copy of each key in a source bucket to a target bucket. At first I tried the script on my local machine but stopped that due to my slow internet connection. Soooo I went over to EC2 and started an instance, configured it and tadaa,.., got everything work.

Here is the way to go:

First we have to start the EC2 instance and install some additional packages:

ec2-add-keypair <name>-keypair
# copy the result to ./~ssh/id_rsa-<name>-keypair
chmod 700 ./~ssh/id_rsa-<name>-keypair

ec2-run-instances <instance-identifier, e.g.: ami-0d729464> -k <name>-keypair
# copy the value behind INSTANCE (should be smth. like i-64d4c00f)
ec2-describe-instances <copied value> (refresh until instance has booted)
# once it’s running you will get an url which we use for ssh

ssh -i ~/.ssh/id_rsa-<name>-keypair root@<url>

sudo apt-get update
sudo apt-get install ruby-full build-essential
sudo apt-get install rubygems1.8
sudo gem install right_aws

After that we can just jump right into the Interactive Ruby Shell and rock on:

require “rubygems”
require “right_aws”

KEY = “xx”
SECRET = “xx”
SOURCE_BUCKET = “xx”
TARGET_BUCKET = “xx”

s3 = RightAws::S3.new(KEY, SECRET)
source = s3.bucket(SOURCE_BUCKET)
target = s3.bucket(TARGET_BUCKET, true)

source.keys.each{ |key| target.put(key.name, key.data) }

Hope this might help someone out there. Ah and one further notice: All data duplicated seems to dive directly into the RAM of the machine, so you probably don’t want to copy all of the keys in a single step.