title: Amazon S3 Signature Does Not Match - AWS SDK Java
body: <p>I have a play application that needs to upload files to S3. We are developing in scala and using the Java AWS SDK.</p>
  
  <p>I'm having trouble trying to upload files, I keep getting 403 SignatureDoesNotMatch when using  presigned urls. The url is being genereated using AWS Java SDK by the following code:</p>
  
  <pre><code>def generatePresignedPutRequest(filename: String) = {
      val expiration = new java.util.Date();
      var msec = expiration.getTime() + 1000 * 60 * 60; // Add 1 hour.
      expiration.setTime(msec);
  
      s3 match {
        case Some(s3) =&gt; s3.generatePresignedUrl(bucketname, filename, expiration, HttpMethod.PUT).toString
        case None =&gt; {
          Logger.warn("S3 is not availiable. Cannot generate PUT request.")
          "URL not availiable"
        }
      }
    }
  </code></pre>
  
  <p>For the frontend code we followed <a href="http://www.ioncannon.net/programming/1539/direct-browser-uploading-amazon-s3-cors-fileapi-xhr2-and-signed-puts/" rel="noreferrer">ioncannon article</a>.</p>
  
  <p>The js function that uploads the file (the same as the one used in the article)</p>
  
  <pre><code> function uploadToS3(file, url)
       {
         var xhr = createCORSRequest('PUT', url);
         if (!xhr) 
         {
           setProgress(0, 'CORS not supported');
         }
         else
         {
           xhr.onload = function() 
           {
             if(xhr.status == 200)
             {
               setProgress(100, 'Upload completed.');
             }
             else
             {
               setProgress(0, 'Upload error: ' + xhr.status);
             }
           };
  
           xhr.onerror = function() 
           {
             setProgress(0, 'XHR error.');
           };
  
           xhr.upload.onprogress = function(e) 
           {
             if (e.lengthComputable) 
             {
               var percentLoaded = Math.round((e.loaded / e.total) * 100);
               setProgress(percentLoaded, percentLoaded == 100 ? 'Finalizing.' : 'Uploading.');
             }
           };
  
           xhr.setRequestHeader('Content-Type', 'image/png');
           xhr.setRequestHeader('x-amz-acl', 'authenticated-read');
  
           xhr.send(file);
         }
       }
  </code></pre>
  
  <p>The server's response is</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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;StringToSignBytes&gt;50 55 bla bla bla...&lt;/StringToSignBytes&gt;
  &lt;RequestId&gt;F7A8F1659DE5909C&lt;/RequestId&gt;
  &lt;HostId&gt;q+r+2T5K6mWHLKTZw0R9/jm22LyIfZFBTY8GEDznfmJwRxvaVJwPiu/hzUfuJWbW&lt;/HostId&gt;
  &lt;StringToSign&gt;PUT
  
      image/png
      1387565829
      x-amz-acl:authenticated-read
      /mybucketname/icons/f5430c16-32da-4315-837f-39a6cf9f47a1&lt;/StringToSign&gt;
  &lt;AWSAccessKeyId&gt;myaccesskey&lt;/AWSAccessKeyId&gt;&lt;/Error&gt;
  </code></pre>
  
  <p>I have configured CORS, double checked aws credentials and tried changing request headers. I always get the same result.
  Why is Amazon telling me that signatures dont match?</p>