title: PHP AWS api raw request to PUT bucket lifecycle
body: <p>I am creating a website, in which there is a feature that if user delete a image/video it will be archived, I am using AWS S3 for storing and on delete want to move it on Glacier, I dont want to use AWS SDK, so i am creating Raw request using PHP cURL, from this link i tried to Put bucket lifecycle on an object, <a href="http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html" rel="nofollow noreferrer">http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketPUTlifecycle.html</a> and done some code, but it give me error of mismatch signature,</p>
  
  <p><code>SignatureDoesNotMatch-The request signature we calculated does not match the signature you provided. Check your key and signing method.</code></p>
  
  <p>This is my code, in this i want to apply lifecycle on <code>x.php</code> which is inside a bucket, apply lifecycle for expire it, What i am doing wrong? Help me,</p>
  
  <pre><code>$AWSaccessKey = 'xxxxxxxxxxxxxxxx';
  $AWSsecretKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
  $AWSregion = 'xxxxxxxxx';
  
  // bucket
  $bucket   = 'xxxxxxxx';
  $postdata = $filedata = '&lt;LifecycleConfiguration&gt;
    &lt;Rule&gt;
      &lt;Filter&gt;
        &lt;Prefix&gt;/&lt;/Prefix&gt;
      &lt;/Filter&gt;
      &lt;Status&gt;Enabled&lt;/Status&gt;
      &lt;Expiration&gt;
        &lt;Days&gt;0&lt;/Days&gt;
      &lt;/Expiration&gt;
    &lt;/Rule&gt;
  &lt;/LifecycleConfiguration&gt;';
  $filetype = 'text/plain';
  $path     = '/x.php'; // file on which i want to put lifecycle to move it to GLACIER
  
  // file md5
  $file_md5 = base64_encode(md5($filedata, true));
  
  // file size
  $filesize = strlen($filedata);
  
  // date
  $date = gmdate('D, d M Y H:i:s').' +0000';
  
  // -&gt; for putting lifecycle config
  $params = array(
      'x-amz-date'          =&gt; gmdate('D, d M Y H:i:s \\G\\M\\T'),
  );
  //'x-amz-security-token'=&gt; $auth['Token']
  
  // sort and stringify params (different to other requests, this is formatted like headers)
  $params_str = '';
  uksort($params, 'strcmp');
  foreach($params as $k=&gt;$v){
      $params_str .= $k.': '.$v."\\n";
  }
  
  // -&gt; for putting lifecycle config
  $to_sign = "PUT\\n$file_md5\\n$filetype\\n\\n".$params_str.'/'.$bucket.$path;
  
  // create signature
  // Note: S3 uses SHA1 instead of 256!
  $signature = base64_encode(hash_hmac('SHA1', $to_sign, $AWSsecretKey, true));
  
  $headers = "Host: $bucket.s3.amazonaws.com\\n"; // change to your region
  $headers .= $params_str;  // note that the params get added to the header
  $headers .= 'Content-MD5: '.$file_md5."\\n";
  $headers .= 'Authorization: AWS '.$AWSaccessKey.':'.$signature."\\n";
  $headers .= 'Content-Length: '.$filesize."\\n";
  
  $ch = curl_init("http://$bucket.s3-$AWSregion.amazonaws.com");
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_HTTPHEADER, explode('\n', $headers));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_STDERR, fopen(dirname(__FILE__).'/errorlog.txt', 'w'));
  
  $result = curl_exec($ch); 
  var_dump($result);
  </code></pre>