title: Obtain S3 presigned post url with query parameters for a mobile client
body: <p>I am creating an API for a backend service with Rails 4.
  The service needs to upload an image file to an amazon s3 bucket.</p>
  
  <p>I'd like to use a direct upload url, so that the clients manage the uploads to s3 and the server is not kept busy.</p>
  
  <p>Currently I have the following prototypical rails action</p>
  
  <pre><code>def create
    filename = params[:filename]
    s3_direct_post = S3_BUCKET.presigned_post(key: "offers/#{SecureRandom.uuid}/#{filename}", acl: 'public-read')
    s3p = s3_direct_post.fields
    url = "#{s3_direct_post.url}/#{filename}?X-Amz-Algorithm=#{s3p['x-amz-algorithm']}&amp;X-Amz-Credential=#{s3p['x-amz-credential']}&amp;X-Amz-Date=#{s3p['x-amz-date']}&amp;X-Amz-Expires=3600&amp;X-Amz-SignedHeaders=host&amp;X-Amz-Signature=#{s3p['x-amz-signature']}"
    render json: {success: true, url: url}, status: :ok
  end
  </code></pre>
  
  <p>This generates such an url:</p>
  
  <pre><code>https://my-bucket.s3.eu-central-1.amazonaws.com/test.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&amp;X-Amz-Credential=MYKEY/20150420/eu-central-1/s3/aws4_request&amp;X-Amz-Date=20150420T162603Z&amp;X-Amz-Expires=3600&amp;X-Amz-SignedHeaders=host&amp;X-Amz-Signature=MYSIGNATURE
  </code></pre>
  
  <p>Now I try to post the test.png to this url with the following:</p>
  
  <p><code>curl -v -T test.png "url"</code></p>
  
  <p>and I get the following error response:</p>
  
  <pre><code>&lt;Error&gt;&lt;Code&gt;SignatureDoesNotMatch&lt;/Code&gt;&lt;Message&gt;The request signature we calculated does not match the signature you provided. Check your key and signing method.&lt;/Message&gt;&lt;AWSAccessKeyId&gt;MYKEY&lt;/AWSAccessKeyId&gt;...
  </code></pre>
  
  <p>I believe the problem comes from the fact, that the specified X-Amz-SignedHeaders Header is wrong. I am not sure which headers are used by default from the amazon rails sdk gem. </p>
  
  <p><em>How should I change my url generation, so that a mobile client can just take the url and post a file to it?</em></p>