3

I currently have an app that allows uploads to AWS S3, the upload is handled purely from the front-end of the app in js with the aws-sdk. We had some users facing this issue mentioned in the title (The difference between the request time and the current time is too large) which prevents them from uploading properly.

I am aware of the solutions provided here, but I was wondering if there was anything I could do to make sure this doesn't happen again for any of the users, with front-end changes only. Is there a way I can make my request be in sync properly ?

I tried having some users sync their clock, but it either did not work or they didn't do it properly. Unfortunately I cannot rely on the user to fix this.

Community
  • 1
  • 1
G4bri3l
  • 4,996
  • 4
  • 31
  • 53
  • You could have your app collect their local time and check it against a server before allowing an upload, alerting the user if the check fails, and disabling the "upload" functionality –  Apr 26 '17 at 18:15

2 Answers2

8

I faced similar issue with S3 upload and on Cognito. Try adding correctClockSkew: true in the config block where you set accessKeyId and secretAccessKey. Like below:

AWS.config.update({
accessKeyId: awsCredentials.ACCESS_KEY_ID,
secretAccessKey: awsCredentials.SECRET_KEY_ID,
correctClockSkew: true,  
});
Sajal
  • 1,198
  • 1
  • 17
  • 32
  • Thanks! Works for me using the solution https://stackoverflow.com/a/43642178/1141612 and your solution. – Miguel Lara Apr 11 '19 at 11:57
  • Does this require exposing the Date header too? – Tom Mar 31 '20 at 18:47
  • This worked wonders for us too. Our use case was downloading many (4k) 50MB(ish) files from S3. We kept seeing OP's error around the 15-16 minute mark. I wish AWS defaulted this to true!!!!!!!!!! – notAChance Feb 16 '22 at 18:23
4

From: https://github.com/aws/aws-sdk-js/issues/399#issuecomment-233057244

Try this:

AWS.events.on('retry', function(response) {  
      if (response.error.name === 'RequestTimeTooSkewed') {
        console.error('User time is not correct. Handling error!');
        console.log('AWS systemClockOffset:', AWS.config.systemClockOffset);
        var serverTime = Date.parse(response.httpResponse.headers['date']);
        var timeNow = new Date().getTime();
        console.log('AWS timestamp:', new Date(serverTime));
        console.log('Browser timestamp:', new Date(timeNow));
        AWS.config.systemClockOffset = Math.abs(timeNow - serverTime);
        response.error.retryable = true;
        console.log('Setting systemClockOffset to:', AWS.config.systemClockOffset);
        console.log('Retrying uploading to S3 once more...');
      }
});

You detect how much the clock is off by, set AWS.config.systemClockOffset to the difference, and retry

  • 1
    In addition to this, one needs to add `Date` into the CORS config of the bucket to allow exposing the date field. Thanks for the help! – G4bri3l Apr 26 '17 at 20:00
  • if i use Aws SDK 2.7.27 written in PHP, where do i insert the code above? is it inside the SDK or inside the AWS console? where? – Rodniko Oct 21 '20 at 08:29
  • It's not inside the SDK, this code is used on the client consuming the SDK. – G4bri3l Dec 08 '20 at 17:06
  • events property is not available in aws-sdk anymore - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#systemClockOffset-property – Ajitesh Singh Feb 01 '21 at 11:46