Class: SimpleAWS::DynamoDB

Inherits:
API
  • Object
show all
Defined in:
lib/simple_aws/dynamo_db.rb

Overview

Amazon's DynamoDB NoSQL Store

http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/Introduction.html

All requests are POST and always through HTTPS. Use the third parameter to initialize if you need to talk to a region other than us-east-1.

This module hooks up the method_missing functionality as described in the README. To call methods on APIs including this module, simply call a method with either the Ruby-fied name, or the full CamelCase name, and pass in options required as the parameters.

As all API calls to DynamoDB require a session token header garnered through STS, you don't have to worry about it. This API will take care of the STS hop and ensure the proper credentials are passed into DynamoDB as needed.

With that, the only parameter you need to pass into your API call directly is the body of the request, which can be a Hash containing keys and values serializable to JSON or a raw JSON string that will be sent directly to Amazon:

dynamo_db.delete_table "TableName" => "Table1"

dynamo_db.delete_table "{'TableName': 'Table1'}"

Note: It is possible right now that if you have a single instance of this API for a long period that the session_token will eventually expire. If this becomes and issue please open an Issue on Github and I'll look at making this handling more robust. You can always recreate a new instance of SimpleAWS::DynamoDB to get new STS credentials as needed.

See Also:

Instance Attribute Summary (collapse)

Attributes inherited from API

#access_key, #region, #secret_key, #version

Instance Method Summary (collapse)

Methods inherited from API

default_region, endpoint, #uri, use_https, version

Constructor Details

- (DynamoDB) initialize(access_key, secret_key, region = nil)

Initialize a new instance of this API, swapping out access_key and secret_key with values from the Security Token Service (STS). This also will grab and store the session_token value for use in DynamoDB API calls.

See Also:



57
58
59
60
61
62
63
64
# File 'lib/simple_aws/dynamo_db.rb', line 57

def initialize(access_key, secret_key, region = nil)
  @sts = SimpleAWS::STS.new access_key, secret_key

  sts_response = @sts.get_session_token.credentials
  @session_token = sts_response.session_token

  super(sts_response.access_key_id, sts_response.secret_access_key, region)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(name, *args)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/simple_aws/dynamo_db.rb', line 66

def method_missing(name, *args)
  request = SimpleAWS::Request.new :post, self.uri, "/"
  target = "DynamoDB_#{self.version.gsub("-","")}"
  request.headers["Content-Type"] = "application/x-amz-json-1.0"

  request.headers["x-amz-target"] = "#{target}.#{SimpleAWS::Util.camelcase(name.to_s)}"
  request.headers["x-amz-date"] = Time.now.rfc822
  request.headers["x-amz-security-token"] = @session_token

  body = args.first || {}
  request.body = body.is_a?(String) ? body : MultiJson.encode(body)

  request.headers["x-amzn-authorization"] =
    "AWS3 AWSAccessKeyId=#{self.access_key}," +
    "Algorithm=HmacSHA256," +
    "Signature=#{build_signature_for(request)}"

  connection = SimpleAWS::Connection.new
  connection.call request
end

Instance Attribute Details

- (Object) session_token (readonly)

Returns the value of attribute session_token



47
48
49
# File 'lib/simple_aws/dynamo_db.rb', line 47

def session_token
  @session_token
end

- (Object) sts (readonly)

Returns the value of attribute sts



47
48
49
# File 'lib/simple_aws/dynamo_db.rb', line 47

def sts
  @sts
end