AWS::S3 is a Ruby library for Amazon's Simple Storage Service's REST API (http://aws.amazon.com/s3).
Full documentation of the currently supported API can be found at http://docs.amazonwebservices.com/AmazonS3/2006-03-01.
== Getting started
To get started you need to require 'aws/s3':
% irb -rubygems
irb(main):001:0> require 'aws/s3'
# => true
The AWS::S3 library ships with an interactive shell called <tt>s3sh</tt>. From within it, you have access to all the operations the library exposes from the command line.
% s3sh
>> Version
Before you can do anything, you must establish a connection using Base.establish_connection!. A basic connection would look something like this:
AWS::S3::Base.establish_connection!(
:access_key_id => 'abc',
:secret_access_key => '123'
)
The minimum connection options that you must specify are your access key id and your secret access key.
(If you don't already have your access keys, all you need to sign up for the S3 service is an account at Amazon. You can sign up for S3 and get access keys by visiting http://aws.amazon.com/s3.)
For convenience, if you set two special environment variables with the value of your access keys, the console will automatically create a default connection for you. For example:
By default buckets are private. This means that only the owner has access rights to the bucket and its objects.
Objects in that bucket inherit the permission of the bucket unless otherwise specified. When an object is private, the owner can
generate a signed url that exposes the object to anyone who has that url. Alternatively, buckets and objects can be given other
access levels. Several canned access levels are defined:
* <tt>:private</tt> - Owner gets FULL_CONTROL. No one else has any access rights. This is the default.
* <tt>:public_read</tt> - Owner gets FULL_CONTROL and the anonymous principal is granted READ access. If this policy is used on an object, it can be read from a browser with no authentication.
* <tt>:public_read_write</tt> - Owner gets FULL_CONTROL, the anonymous principal is granted READ and WRITE access. This is a useful policy to apply to a bucket, if you intend for any anonymous user to PUT objects into the bucket.
* <tt>:authenticated_read</tt> - Owner gets FULL_CONTROL, and any principal authenticated as a registered Amazon S3 user is granted READ access.
You can set a canned access level when you create a bucket or an object by using the <tt>:access</tt> option:
S3Object.store(
'kiss.jpg',
data,
'marcel',
:access => :public_read
)
Since the image we created is publicly readable, we can access it directly from a browser by going to the corresponding bucket name
and specifying the object's key without a special authenticated url:
http://s3.amazonaws.com/marcel/kiss.jpg
==== Building custum access policies
For both buckets and objects, you can use the <tt>acl</tt> method to see its access control policy:
policy = S3Object.acl('kiss.jpg', 'marcel')
pp policy.grants
[#<AWS::S3::ACL::Grant FULL_CONTROL to noradio>,
#<AWS::S3::ACL::Grant READ to AllUsers Group>]
Policies are made up of one or more grants which grant a specific permission to some grantee. Here we see the default FULL_CONTROL grant
to the owner of this object. There is also READ permission granted to the Allusers Group, which means anyone has read access for the object.
Say we wanted to grant access to anyone to read the access policy of this object. The current READ permission only grants them permission to read
the object itself (for example, from a browser) but it does not allow them to read the access policy. For that we will need to grant the AllUsers group the READ_ACP permission.
First we'll create a new grant object:
grant = ACL::Grant.new
# => #<AWS::S3::ACL::Grant (permission) to (grantee)>
grant.permission = 'READ_ACP'
Now we need to indicate who this grant is for. In other words, who the grantee is:
grantee = ACL::Grantee.new
# => #<AWS::S3::ACL::Grantee (xsi not set yet)>
There are three ways to specify a grantee: 1) by their internal amazon id, such as the one returned with an object's Owner,
2) by their Amazon account email address or 3) by specifying a group. As of this writing you can not create custom groups, but
Amazon does provide three already: AllUsers, Authenticated and LogDelivery. In this case we want to provide the grant to all users.
This effectively means "anyone".
grantee.group = 'AllUsers'
Now that our grantee is setup, we'll associate it with the grant:
grant.grantee = grantee
grant
# => #<AWS::S3::ACL::Grant READ_ACP to AllUsers Group>
Are grant has all the information we need. Now that it's ready, we'll add it on to the object's access control policy's list of grants:
policy.grants << grant
pp policy.grants
[#<AWS::S3::ACL::Grant FULL_CONTROL to noradio>,
#<AWS::S3::ACL::Grant READ to AllUsers Group>,
#<AWS::S3::ACL::Grant READ_ACP to AllUsers Group>]
Now that the policy has the new grant, we reuse the <tt>acl</tt> method to persist the policy change:
S3Object.acl('kiss.jpg', 'marcel', policy)
If we fetch the object's policy again, we see that the grant has been added:
pp S3Object.acl('kiss.jpg', 'marcel').grants
[#<AWS::S3::ACL::Grant FULL_CONTROL to noradio>,
#<AWS::S3::ACL::Grant READ to AllUsers Group>,
#<AWS::S3::ACL::Grant READ_ACP to AllUsers Group>]
If we were to access this object's acl url from a browser:
http://s3.amazonaws.com/marcel/kiss.jpg?acl
we would be shown its access control policy.
==== Pre-prepared grants
Alternatively, the ACL::Grant class defines a set of stock grant policies that you can fetch by name. In most cases, you can
just use one of these pre-prepared grants rather than building grants by hand. Two of these stock policies are <tt>:public_read</tt>
and <tt>:public_read_acp</tt>, which happen to be the two grants that we built by hand above. In this case we could have simply written:
A bucket can be set to log the requests made on it. By default logging is turned off. You can check if a bucket has logging enabled:
Bucket.logging_enabled_for? 'jukebox'
# => false
Enabling it is easy:
Bucket.enable_logging_for('jukebox')
Unless you specify otherwise, logs will be written to the bucket you want to log. The logs are just like any other object. By default they will start with the prefix 'log-'. You can customize what bucket you want the logs to be delivered to, as well as customize what the log objects' key is prefixed with by setting the <tt>target_bucket</tt> and <tt>target_prefix</tt> option:
Bucket.enable_logging_for(
'jukebox', 'target_bucket' => 'jukebox-logs'
)
Now instead of logging right into the jukebox bucket, the logs will go into the bucket called jukebox-logs.
Once logs have accumulated, you can access them using the <tt>logs</tt> method: