Skip to content

Instantly share code, notes, and snippets.

View trevorrowe's full-sized avatar

Trevor Rowe trevorrowe

  • Amazon.com
  • Seattle, WA
View GitHub Profile
@trevorrowe
trevorrowe / enable logging
Created March 22, 2012 21:45
Enabling logging in the aws-sdk gem
require 'aws-sdk'
require 'logger'
# configure a logger instance that logs to standard out,
# this could be a file, standard error, etc. The logger only
# needs to respond to #log and accept a severity and a message.
AWS.config(:logger => Logger.new($stdout))
# make a request to see the request logger
ec2 = AWS::EC2.new
@trevorrowe
trevorrowe / debug handler example
Created March 22, 2012 21:34
Wrapping the aws-sdk http handler to view raw http requests and responses
# build a dummy http handler that extends the default handler
# that outputs the request body before making the request (via super)
# then check the response body
# req is a AWS::Core::Http::Request object
# resp is a AWS::Core::Http::Response object
default_handler = AWS.config.http_handler
debug_handler = AWS::Core::Http::Handler.new(default_handler) do |req,resp|
puts "REQUEST BODY: #{req.body}"
super(req,resp)
@trevorrowe
trevorrowe / progress.rb
Created September 14, 2015 18:51
Tracking progress of a file upload using v2 of the aws-sdk gem
require 'aws-sdk'
require 'pathname'
class ProgressIO
def initialize(io)
@io = io
@bytes_read = 0
@listeners = []
end
@trevorrowe
trevorrowe / gist:bb041b0d7b57df239d43
Created June 29, 2015 20:59
Expect-100 continue PUT bug - sever
require 'net/http'
require 'logger'
req = Net::HTTP::Put.new('/', { 'expect' => '100-continue' })
req.body = 'data'
http = Net::HTTP.new('localhost', 3000)
http.continue_timeout = 1
http.set_debug_output(Logger.new($stdout))
http.request(req)
@trevorrowe
trevorrowe / gist:de95d25baa0d472d71da
Created June 29, 2015 20:59
Expect-100 continue PUT bug - client
# This example server accepts a simple PUT request with the 'Excpect: 100-continue'
# header. It alternates between the following two responses:
#
# * 100 Continue, accepting the body, then 200 OK
# * 403 Forbidden, not accepting the body
#
require 'socket'
server = TCPServer.new('localhost', 3000)
@trevorrowe
trevorrowe / gist:8162733833c90491a9b3
Last active August 29, 2015 14:23
Plugin that conditionally raises response errors
class ConditionalRaisePlugin < Seahorse::Client::Plugin
class Handler < Seahorse::Client::Handler
def call(context)
response = @handler.call(context)
conditional_raise(response.error, context.config.errors_to_ignore)
response
end
class ProgressIO
def initialize(io)
@io = io
end
def read(bytes = nil, output_buffer = nil)
puts bytes
@io.read(bytes, output_buffer)
end
pattern = "[aws] [:client_class] operation=:operation duration=:time retries=:retries error=:error_class\n"
Aws.config[:log_formatter] = Seahorse::Client::Logging::Formatter.new(pattern)
# success
[aws] [Aws::S3::Client] operation=list_buckets duration=0.602057 retries=0 error=
# error
[aws] [Aws::S3::Client] operation=head_bucket duration=1.100552 retries=0 error=Aws::S3::Errors::Forbidden
@trevorrowe
trevorrowe / gist:e03c727941728320f15b
Created March 14, 2015 06:33
Configuring an Amazon S3 Encryption Client
client = Aws::S3::Client.new(credentials: Aws::Credentials.new('akid', 'secret'))
encryption_client = Aws::S3::Encryption::Client.new(client: client)
shape = sqs.config.api.operation(:receive_message).output
shape = Seahorse::Model::Shapes::Structure.new({
'members' => {
shape.metadata('resultWrapper') => shape.definition,
'ResponseMetadata' => {
'type' => 'structure',
'members' => {
'RequestId' => { 'type' => 'string' }
}
}