title: Amazon S3 Query String Authentication Invalid Date
body: <p>I found this script for creating links to Amazon S3 resources using the Query String Authentication: <a href="https://gist.github.com/1032395" rel="nofollow">https://gist.github.com/1032395</a></p>
  
  <p>Since I'm not using rails I included the libraries manually.</p>
  
  <p>Everytime I try opening a resource using an URL generated by this script I get an "access denied"-error because of a "Invalid date (should be seconds since epoch): 1349364847"</p>
  
  <p>Any ideas where this could come from?</p>
  
  <pre><code>require 'cgi'
  require 'base64'
  require 'openssl'
  
  def generate_secure_s3_url(s3_key)
      #
      # s3_key would be a path (including filename) to the file like:  "folder/subfolder/filename.jpg"
      # but it should NOT contain the bucket name or a leading forward-slash
      #
      # this was built using these instructions:
      # http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?S3_QSAuth.html
      # http://aws.amazon.com/code/199?_encoding=UTF8&amp;jiveRedirect=1
  
      s3_base_url       = '' # i.e. https://mybucket.s3.amazonaws.com
      bucket            = '' # i.e. mybucket
      access_key_id     = '' # your Amazon S3 access key ID
      secret_access_key = '' # your Amazon S3 secret access key
      expiration_date   = Time.now.utc.to_i + (2*24*60*60) # 2 days from now in UTC epoch time (i.e. 1308172844)
  
      # this needs to be formatted exactly as shown below and UTF-8 encoded
      string_to_sign = "GET\n\n\n#{expiration_date}\n/#{bucket}/#{s3_key}".encode("UTF-8")
  
      # we have to CGI/URL escape the signature since it would fail if it included / or + characters
      signature = CGI.escape(Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'), secret_access_key, string_to_sign)).gsub("\n",""))
  
      return "#{s3_base_url}/#{s3_key}?AWSAccessKeyId=#{access_key_id}
                                      &amp;Expires=#{expiration_date}
                                      &amp;Signature=#{signature}"
  end
  </code></pre>