Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After gathering the individual pieces of this puzzle, I was able to create this Ruby method that properly signs a query string url using the aws secret key.</p> <p>My resources for this:</p> <ul> <li><a href="http://docs.amazonwebservices.com/AmazonS3/latest/API/index.html?RESTObjectGET.html" rel="nofollow">RESTObjectGET documentation</a></li> <li><a href="http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAuthentication.html#RESTAuthenticationQueryStringAuth" rel="nofollow">Signing and Authenticating REST Requests</a></li> </ul> <p>Also, the response back from S3 was helpful, because when I created a url with a bad signature, the response showed the string_to_sign that AWS S3 generated from deciphering the URL I generated. After a few iterations I was able to converge on the correct formatting of the string_to_sign and after that it was pretty standard stuff.</p> <p>Here is the Ruby method:</p> <pre><code>############################################################################## # Create a signed query-string URL that supports setting response headers ############################################################################## def s3_signed_url(bucket, pathname, verb, content_md5=nil, content_type=nil, response_headers = {}) expires = Time.now + 5.minutes response_headers_canonicalized = response_headers.sort_by{|key, value| key.downcase}.collect{|key, value| "#{key}=#{value}"}.join("&amp;").to_s string_to_sign = "#{verb.upcase}\n#{content_md5}\n#{content_type}\n#{expires.to_i}\n/#{bucket}/#{pathname}?#{response_headers_canonicalized}" digest = OpenSSL::Digest::Digest.new('sha1') hmac = OpenSSL::HMAC.digest(digest, aws_secret_key, string_to_sign) signature = Base64.encode64(hmac).chomp url = "http://s3.amazonaws.com/#{bucket}/#{pathname}?" if response_headers.count &gt; 0 response_headers.each do |key, value| url += "#{key}=#{value}&amp;" end end url += "AWSAccessKeyId=#{aws_access_key}&amp;Expires=#{expires.to_i}&amp;Signature=#{CGI.escape(signature)}"; return url end </code></pre> <p>And you call the method like this:</p> <pre><code>file_url_s3 = s3_signed_url(file_bucket, file_path, 'GET', nil, nil, {'response-content-disposition' =&gt; 'attachment'}) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload