How do I solve the AWS error: "The difference between the request time and the current time is too large" front-end only?

How do I solve the AWS error: "The difference between the request time and the current time is too large" front-end only?

我目前有一个允许上传到 AWS S3 的应用程序,上传完全由带有 aws-sdk 的 js 应用程序的 front-end 处理。我们有一些用户面临标题中提到的这个问题 (The difference between the request time and the current time is too large),这导致他们无法正常上传。

我知道 here 提供的解决方案,但我想知道我是否可以采取任何措施来确保这种情况不会再次发生在 front-end 的任何用户身上仅更改。有没有办法让我的请求正确同步?

我试过让一些用户同步他们的时钟,但要么不起作用,要么他们没有正确完成。不幸的是,我不能指望用户来解决这个问题。

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

试试这个:

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...');
      }
});

您检测时钟偏移了多少,将AWS.config.systemClockOffset设置为差值,然后重试

我在 S3 上传和 Cognito 上遇到了类似的问题。尝试在设置 accessKeyId 和 secretAccessKey 的配置块中添加 correctClockSkew: true。如下所示:

AWS.config.update({
accessKeyId: awsCredentials.ACCESS_KEY_ID,
secretAccessKey: awsCredentials.SECRET_KEY_ID,
correctClockSkew: true,  
});