MoMolog MoMolog - 3 months ago 19
Ruby Question

Set up S3 bucket as website using ruby SDK

I want to set up an Amazon S3 bucket as a website as described here:

http://docs.amazonwebservices.com/AmazonS3/latest/API/RESTBucketPUTwebsite.html?r=5271

but using an ruby API, preferably the aws-sdk for ruby.
Is there a possibility to do that / a library that already supports that? Could not find anything in aws-sdk and right-aws, but maybe I was just blind?

Answer

It is possible to configure your bucket as website using an ruby API. I did found a solution, but this uses the aws-s3 gem, not the aws-sdk gem. I found this solution in the ponyhost gem:

body = '<?xml version="1.0" encoding="UTF-8"?>
<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <IndexDocument><Suffix>index.html</Suffix></IndexDocument>
  <ErrorDocument><Key>404.html</Key></ErrorDocument>
</WebsiteConfiguration>'

res = AWS::S3::Base.request(:put, "/#{bucketname}?website", {}, body)

EDIT you could also use the fog gem to accomplish this.

storage = Fog::Storage.new({
  :provider   => 'AWS',
  :aws_access_key_id => 'YOUR_KEY',
  :aws_secret_access_key => 'YOUR_SECRET',
  :region => 'eu-west-1'
})

if storage.kind_of?(Fog::Storage::AWS::Real)
  storage.put_bucket_website("YOUR_BUCKET", "index.html", :key => "404.html")
end