如何解决 AWS 错误:“请求时间与当前时间之间的差异太大”仅限前端?

问题描述 投票:0回答:3

我目前有一个允许上传到AWS S3的应用程序,上传纯粹是从应用程序的前端在js中使用

aws-sdk
处理的。我们有一些用户面临标题中提到的这个问题(
The difference between the request time and the current time is too large
),这导致他们无法正确上传。

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

我尝试让一些用户同步他们的时钟,但要么不起作用,要么他们没有正确执行。不幸的是,我不能依赖用户来解决这个问题。

javascript amazon-web-services amazon-s3 frontend
3个回答
8
投票

我在 S3 上传和 Cognito 上遇到了类似的问题。尝试在设置 accessKeyId 和 SecretAccessKey 的配置块中添加 CorrectClockSkew: true 。就像下面这样:

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

4
投票

来自: 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 设置为差异,然后重试


0
投票

此错误有时可能是由于尝试并行运行多个请求(如果它们很大/很耗时)而导致的。在这种情况下,这不是实际时钟偏差的问题,而是请求准备(并用时间戳签名)与实际出列和发送之间的时间差的问题。请参阅https://stackoverflow.com/a/77680140/6505513了解完整的解释和解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.