Here is an example on how to authenticate with the AWS SDK for Ruby:
Code Block |
---|
language | ruby |
---|
title | Signing AWS API request |
---|
|
require 'net/http'
require 'uri'
require 'json'
require 'aws-sigv4'
signer = Aws::Sigv4::Signer.new(
service: 'execute-api',
region: 'eu-west-1',
# static credentials
access_key_id: 'AKIAJ7BIEEA2LJGI73WA',
secret_access_key: '8YS76N7b+tLxJRohTcrF8eppO5BBDn0EDvgVM9LD',
)
uri = URI('https://example.execute-api.eu-west-1.amazonaws.com/test/foo')
headers = {
'Content-Type' => 'application/json; charset=utf8',
}
signature = signer.sign_request(
http_method: 'GET',
url: uri,
headers: headers,
body: nil # String or IO object
)
headers['host'] = signature.headers['host']
headers['x-amz-date'] = signature.headers['x-amz-date']
headers['x-amz-security-token'] = signature.headers['x-amz-security-token']
headers['x-amz-content-sha256'] = signature.headers['x-amz-content-sha256']
headers['authorization'] = signature.headers['authorization']
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri, headers)
response = http.request(request)
body = JSON.parse(response.body)
|