title: AWS S3 Access Denied Only Sometimes
body: <p>I have been trying for days to get upload file from the aws-sdk for javascript to an s3 bucket. I have added the correct CORS policy and for testing purposes the bucket has public permissions. File upload only works 50% of the time though.</p>
  
  <p>I am trying to upload 3-4 files in immediate succession from my website but I am getting access denied after the 2nd one. For some reason the first 2 upload successfully but the 3rd onwards fail. I have no idea if i have uncovered a glitch or if there is something I have done wrong. </p>
  
  <p>My CORs policy is. I have also tried </p>
  
  <pre><code>`&lt;CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"&gt;
  &lt;CORSRule&gt;
      &lt;AllowedOrigin&gt;http://*&lt;/AllowedOrigin&gt;
      &lt;AllowedOrigin&gt;https://*&lt;/AllowedOrigin&gt;
      &lt;AllowedMethod&gt;PUT&lt;/AllowedMethod&gt;
      &lt;AllowedMethod&gt;POST&lt;/AllowedMethod&gt;
      &lt;AllowedMethod&gt;GET&lt;/AllowedMethod&gt;
      &lt;AllowedMethod&gt;DELETE&lt;/AllowedMethod&gt;
      &lt;MaxAgeSeconds&gt;3000&lt;/MaxAgeSeconds&gt;
      &lt;ExposeHeader&gt;ETag&lt;/ExposeHeader&gt;
      &lt;AllowedHeader&gt;*&lt;/AllowedHeader&gt;
  &lt;/CORSRule&gt;
  &lt;/CORSConfiguration&gt;` 
  </code></pre>
  
  <p>I have also tried:</p>
  
  <pre><code>&lt;AllowedOrigin&gt;*&lt;/AllowedOrigin&gt;
  </code></pre>
  
  <p>To no success.</p>
  
  <p>To upload files I use temporary credentials from the aws-cognito sdk for js. The code I use to upload files is as follows:</p>
  
  <pre><code>function uploadFile(fileData,callback){
  var file = fileData[4];
  var key = "";
  for(i=0; i&lt;3; i++){
  var temp = Math.ceil(Math.random()*1000000000000)
  key=key+temp;
  }
  
  AWS.config.sessionToken= mySessionToken;
  AWS.config.accessKeyId= myAccessKey;
  AWS.config.secretAccessKey= mySecretKey;
  AWS.config.region= myRegion;
  var bucket = new AWS.S3({params: {Bucket: myBucketName}});
  var params = {Key: key, ContentType: file.type, Body: file, ACL:'bucket-owner-
  full-control'};
  bucket.upload(params, function (err, data) {
  if (err) {
  
  callback(false,err,fileData);
  
  }
  else{
  callback(true,"",fileData);
  }
  });
  
  }
  </code></pre>
  
  <p>If it matters the website I am uploading from is hosted on an ec2 instance.</p>
  
  <p>I know there are alot of posts about how to get file uploads to work with S3 but I have never seen any where it only works a portion of the time. I assume the credentials I use and my CORS configuration is correct because otherwise the uploads would never work right? </p>
  
  <p>Thanks in advance for the help!</p>
  
  <p>Edit: </p>
  
  <p>This error only occurs on the 3rd sequential upload and onwards. The first and second file I upload have no errors and are successful. </p>
  
  <p>The error code I receive is as follows:</p>
  
  <p>Put [S3Instance] 403 Forbidden </p>
  
  <pre><code>&lt;Error&gt;
  &lt;Code&gt;
  AccessDenied
  &lt;/Code&gt;
  &lt;Message&gt;
  Access Denied
  &lt;/Message&gt;
  &lt;RequestId&gt;
  RequestId
  &lt;/RequestId&gt;
  &lt;HostId&gt;
  HostId
  &lt;/HostId&gt;
  &lt;/Error&gt;
  </code></pre>