Drew's Blog - Home - Archive

Static S3 Subdomains: API Gotchas

So, recently, the work I've been doing for activist.io has involved programmatically creating static subdomains using the S3 bucket website ability (plus creating the record sets in Route 53). Unfortunately, the S3 ruby sdk isn't the best, so there are a few gotchas you have to watch out for. Here are the two things I spent the most time figuring out (ruby specific):

First: there is no wrapper method for the "put_bucket_website" endpoint, which you have to use to turn the bucket website ability on and specify your index and error documents for each level of bucket. While it's possible to construct the xml yourself and send this request to AWS by using their lower level request object, the best solution (for projects like this one, with 1.5 developers) is to make someone else maintain the code. (=P) With that in mind, you can use the fog gem (there are others, but we were already using fog to handle our static asset sync), pretty easily:
storage = Fog::Storage.new({
    :provider => 'AWS',
    :aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY'],
    :region => 'us-east-1'
  })

storage.put_bucket_website(bucketname, "index.html", {:key => "error.html"})

Second: when you create your bucket, you have the option to set the ACL, like so:
client.buckets.create(name, :acl => :public_read) Unfortunately, there is no way for you to set the default ACL for files that you upload through the API (as far as I can tell; you can do this through the bucket explorer on the web, though). So after you upload each file, you have to set the ACL again:
index = bucket.objects["index.html"].write(index_file) index.acl = :public_read

Hopefully the AWS ruby SDK will add support for both of these things in the future.

If you've read this far, you should probably follow me on Twitter.

See more posts

Drew writes code for fun and (sometimes) profit. He's currently studying Computer Science at Carnegie Mellon University. He has previously worked at Facebook, Amazon, and a startup called Intersect.