title: Signature generation in Amazon S3 using Javascript
body: <p>I am trying to send a PUT request to an Amazon compatible storage, and I keep getting the following error:</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 Secret Access Key and signing method. For more information, see REST Authentication and SOAP Authentication for details.&lt;/Message&gt;&lt;RequestId&gt;0af9f985:155f11613d1:1732:13&lt;/RequestId&gt;&lt;/Error&gt;"
  </code></pre>
  
  <p>Can someone please tell me how to generate the correct signature using the Secret Access Key and other parameters, in Javascript. I know that I am using the correct credentials, because all other operations supported by Amazon S3 are working, using the Amazon S3 SDK. I went through this link:
  <a href="http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html" rel="nofollow">http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html</a>, but I couldn't find what the method is expecting for each parameters.</p>
  
  <p>Adding the code, that I have written. This is pretty much the same as you see in the Javascript SDK, only the method <code>canonicalizedResource</code>, is more simpler:</p>
  
  <pre><code>    private stringToSign(request: any) {
          var parts = [];
          parts.push(request.headers['method']);
          parts.push(request.headers['Content-MD5'] || '');
          parts.push(request.headers['Content-Type'] || '');
          parts.push(request.headers['presigned-expires'] || '');
  
          var headers = this.canonicalizedAmzHeaders(request);
          if (headers) parts.push(headers);
          parts.push(this.canonicalizedResource(request));
          return parts.join('\n');;
      }
  
      private canonicalizedAmzHeaders(request: any) {
          var amzHeaders = [];
          AWS.util.each(request.headers, function(name) {
              if (name.match(/^x-amz-/i))
                  amzHeaders.push(name);
          });
          amzHeaders.sort(function(a, b) {
              return a.toLowerCase() &lt; b.toLowerCase() ? -1 : 1;
          });
          var parts = [];
          AWS.util.each(amzHeaders, function(name) {
              parts.push(amzHeaders[name].toLowerCase() + ':' + request.headers[amzHeaders[name]]);
          });
          return parts.join('\n');
      }
  
      private canonicalizedResource(request) {
  
          return request.path || '/';
      }
  </code></pre>
  
  <p>I call the method like:</p>
  
  <pre><code>var signature = this.sign(AWS.config.credentials.secretAccessKey, this.stringToSign(request));
  </code></pre>