Skip to content

Instantly share code, notes, and snippets.

@dnewkerk
Created December 16, 2015 21:34
Show Gist options
  • Save dnewkerk/ff1bcebf83fb2f1b58b9 to your computer and use it in GitHub Desktop.
Save dnewkerk/ff1bcebf83fb2f1b58b9 to your computer and use it in GitHub Desktop.
def authorize
encoded_md5 = [[md5].pack("H*")].pack("m0")
auth_date = Time.now.httpdate
amz_headers = "x-amz-date:#{auth_date}\n"
resource = "/#{upload.bucket}/#{upload.filename}?partNumber=#{part_number}&uploadId=upload_id"
string_to_sign =
"PUT" + "\n" + # HTTP-Verb
encoded_md5 + "\n" + # Content-MD5
upload.mime_type + "\n" + # Content-Type
"\n" + # Expires
amz_headers + # CanonicalizedAmzHeaders
resource # CanonicalizedResource
{ auth_header: "AWS #{ENV['AWS_ACCESS_KEY_ID']}:#{sign_string(string_to_sign)}",
auth_date: auth_date,
content_md5: encoded_md5 }
end
def sign_string(string)
Base64.strict_encode64(OpenSSL::HMAC.digest("sha1", ENV["AWS_SECRET_ACCESS_KEY"], string))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment