Class: S3::Signature
- Inherits:
-
Object
- Object
- S3::Signature
- Defined in:
- lib/s3/signature.rb
Overview
Class responsible for generating signatures to requests.
Implements algorithm defined by Amazon Web Services to sign request with secret private credentials
See
docs.amazonwebservices.com/AmazonS3/latest/index.html?RESTAuthentication.html
Class Method Summary collapse
-
.generate(options) ⇒ Object
Generates signature for given parameters.
-
.generate_temporary_url(options) ⇒ Object
Generates temporary URL for given resource.
-
.generate_temporary_url_signature(options) ⇒ Object
Generates temporary URL signature for given resource.
Class Method Details
.generate(options) ⇒ Object
Generates signature for given parameters
Options
-
:host
- Hostname -
:request
- Net::HTTPRequest object with correct headers -
:access_key_id
- Access key id -
:secret_access_key
- Secret access key
Returns
Generated signature string for given hostname and request
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/s3/signature.rb', line 24 def self.generate() request = [:request] access_key_id = [:access_key_id] .merge!(:headers => request, :method => request.method, :resource => request.path) signature = canonicalized_signature() "AWS #{access_key_id}:#{signature}" end |
.generate_temporary_url(options) ⇒ Object
Generates temporary URL for given resource
Options
-
:bucket
- Bucket in which the resource resides -
:resource
- Path to the resouce you want to create a temporary link to -
:access_key
- Access key -
:secret_access_key
- Secret access key -
:expires_at
- Unix time stamp of when the resouce link will expire -
:method
- HTTP request method you want to use on the resource, defaults to GET -
:headers
- Any additional HTTP headers you intend to use when requesting the resource
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/s3/signature.rb', line 81 def self.generate_temporary_url() bucket = [:bucket] resource = [:resource] access_key = [:access_key] expires = [:expires_at].to_i signature = generate_temporary_url_signature() url = "http://#{S3::HOST}/#{bucket}/#{resource}" url << "?AWSAccessKeyId=#{access_key}" url << "&Expires=#{expires}" url << "&Signature=#{signature}" end |
.generate_temporary_url_signature(options) ⇒ Object
Generates temporary URL signature for given resource
Options
-
:bucket
- Bucket in which the resource resides -
:resource
- Path to the resouce you want to create a temporary link to -
:secret_access_key
- Secret access key -
:expires_at
- Unix time stamp of when the resouce link will expire -
:method
- HTTP request method you want to use on the resource, defaults to GET -
:headers
- Any additional HTTP headers you intend to use when requesting the resource
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/s3/signature.rb', line 50 def self.generate_temporary_url_signature() bucket = [:bucket] resource = [:resource] secret_access_key = [:secret_access_key] expires = [:expires_at] headers = [:headers] || {} headers.merge!('date' => expires.to_i.to_s) .merge!(:resource => "/#{bucket}/#{resource}", :method => [:method] || :get, :headers => headers) signature = canonicalized_signature() CGI.escape(signature) end |