Setting content-type for s3 causes signing error #143

Closed
ratbeard opened this Issue Nov 2, 2011 · 1 comment

Comments

Projects
None yet
1 participant
@ratbeard

ratbeard commented Nov 2, 2011

Related to #121.

I'm having the issue mentioned in #121 that dragonfly appears to always be setting an explicit `content-type: binary/octet-stream', whereas the s3 gem appears to be letting amazon do its documented behavior of setting content-type based on the file extension.

I opened a new issue, because if I try and explicitly set a content-type, it errors out, with a bad request signature. I'm using master:

gem 'dragonfly', :git => 'https://github.com/markevans/dragonfly.git'


 Dragonfly[:attachments].store('some content', :path => 'z.pdf')
 => "z.pdf"

Dragonfly[:attachments].store('some content', :path => 'zz00.pdf', :headers => {'x-amz-acl' => 'public-read-write'})
=> "zz00.pdf"


# Error
Dragonfly[:attachments].store('some content', :path => 'zz00.pdf', :headers => {'x-amz-acl' => 'public-read-write', 'Content-type' => 'application/pdf'})
Excon::Errors::Forbidden: Expected(200) <=> Actual(403 Forbidden)
  request => {:connect_timeout=>60, :headers=>{"Content-Length"=>12, "x-amz-meta-extra"=>"BAh7AA", "x-amz-acl"=>"public-read-write", "Content-type"=>"application/pdf", "Date"=>"Wed, 02 Nov 2011 13:12:15 +0000", "Authorization"=>"AWS 0YW6T2GGWHR06PR7FKR2:n7Vjd5xTkHI2HHV9ghQ3kHsYQzs=", "Host"=>"optum-lookbook.s3.amazonaws.com:443"}, :host=>"optum-lookbook.s3.amazonaws.com", :mock=>nil, :path=>"/zz00.pdf", :port=>"443", :query=>nil, :read_timeout=>60, :scheme=>"https", :write_timeout=>60, :body=>#<File:/var/folders/uO/uO80rFmIFiCPnB3TMVxTYKaWUho/-Tmp-/dragonfly20111102-12353-1scuhs1>, :expects=>200, :idempotent=>true, :method=>"PUT"}
  response => #<Excon::Response:0x0000012c046050 @body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 70 64 66 0a 57 65 64 2c 20 30 32 20 4e 6f 76 20 32 30 31 31 20 31 33 3a 31 32 3a 31 35 20 2b 30 30 30 30 0a 78 2d 61 6d 7a 2d 61 63 6c 3a 70 75 62 6c 69 63 2d 72 65 61 64 2d 77 72 69 74 65 0a 78 2d 61 6d 7a 2d 6d 65 74 61 2d 65 78 74 72 61 3a 42 41 68 37 41 41 0a 2f 6f 70 74 75 6d 2d 6c 6f 6f 6b 62 6f 6f 6b 2f 7a 7a 30 30 2e 70 64 66</StringToSignBytes><RequestId>DA873199D33D7473</RequestId><HostId>JssFlEGI49M4ksJqobpseC8XeoHy7cO3AtZgHzXEjp8tEIvdl02hlPqBPbaS1Skq</HostId><SignatureProvided>n7Vjd5xTkHI2HHV9ghQ3kHsYQzs=</SignatureProvided><StringToSign>PUT\n\napplication/pdf\nWed, 02 Nov 2011 13:12:15 +0000\nx-amz-acl:public-read-write\nx-amz-meta-extra:BAh7AA\n/optum-lookbook/zz00.pdf</StringToSign><AWSAccessKeyId>0YW6T2GGWHR06PR7FKR2</AWSAccessKeyId></Error>", @headers={"x-amz-request-id"=>"DA873199D33D7473", "x-amz-id-2"=>"JssFlEGI49M4ksJqobpseC8XeoHy7cO3AtZgHzXEjp8tEIvdl02hlPqBPbaS1Skq", "Content-Type"=>"application/xml", "Transfer-Encoding"=>"chunked", "Date"=>"Wed, 02 Nov 2011 13:12:54 GMT", "Connection"=>"close", "Server"=>"AmazonS3"}, @status=403>
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/excon-0.7.6/lib/excon/connection.rb:194:in `request'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/fog-1.0.0/lib/fog/core/connection.rb:20:in `request'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/fog-1.0.0/lib/fog/aws/storage.rb:366:in `request'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/fog-1.0.0/lib/fog/aws/requests/storage/put_object.rb:43:in `put_object'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bundler/gems/dragonfly-c66ffa14520b/lib/dragonfly/data_storage/s3data_store.rb:45:in `block (2 levels) in store'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bundler/gems/dragonfly-c66ffa14520b/lib/dragonfly/temp_object.rb:95:in `file'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bundler/gems/dragonfly-c66ffa14520b/lib/dragonfly/data_storage/s3data_store.rb:44:in `block in store'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bundler/gems/dragonfly-c66ffa14520b/lib/dragonfly/data_storage/s3data_store.rb:146:in `rescuing_socket_errors'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bundler/gems/dragonfly-c66ffa14520b/lib/dragonfly/data_storage/s3data_store.rb:42:in `store'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bundler/gems/dragonfly-c66ffa14520b/lib/dragonfly/app.rb:90:in `store'
    from (irb):2
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
    from /Users/mike.frawley/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
@ratbeard

This comment has been minimized.

Show comment
Hide comment
@ratbeard

ratbeard Nov 2, 2011

Doing a little debugging, I saw that I was setting the content-type incorrectly:

options = {
  path: path,
  headers: {
  'x-amz-acl' => 'public-read-write', # without this we get a 403
  'content-type' => 'application/pdf'
  }
}

While this works:

options = {
  path: path,
  :content_type => 'application/pdf'
}

# ...
storage = Dragonfly[:attachments]
storage.store(content, options)      

ratbeard commented Nov 2, 2011

Doing a little debugging, I saw that I was setting the content-type incorrectly:

options = {
  path: path,
  headers: {
  'x-amz-acl' => 'public-read-write', # without this we get a 403
  'content-type' => 'application/pdf'
  }
}

While this works:

options = {
  path: path,
  :content_type => 'application/pdf'
}

# ...
storage = Dragonfly[:attachments]
storage.store(content, options)      

@ratbeard ratbeard closed this Nov 2, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment