Custom ACL for S3 buckets and keys.

Recently I realized that s3cmd (ubuntu) does not let you have custom acl's on s3 objects. So I wrote the following ruby script and I thought I could share with you all. Using s3fox, s3hub etc was really painful.

#!/usr/bin/env ruby
require 'rubygems'
require 'aws/s3'

key = ARGV[0] ## they key that you want to add ACL to. e.g. foo.txt
bucket = "<your_bucket_name>"
user_id = "<id_of_theobject>" ## It is a little tricky to get this one.
name = "<display_name_of_the_user>"
perms = "<Permissions you want in the ACL>" ## e.g. READ

AWS::S3::Base.establish_connection!(
  :access_key_id     => "<key_id>",
  :secret_access_key => "<access_key>"
)

policy = AWS::S3::S3Object.acl( key, bucket )
grant = AWS::S3::ACL::Grant.new
grantee = AWS::S3::ACL::Grantee.new
grant.grantee = grantee
grant.permission = perms
policy.grants << grant
grantee.type = "CanonicalUser"
grantee.id = user_id
grantee.display_name = name
AWS::S3::S3Object.acl( key, bucket , policy)

## And thats it, run s3cmd info /<BUCKET>/<key> to see the new ACL.