2017-04-26 31 views
0

我目前有一个允许上传到AWS S3的应用程序,上传完全是从js的aws-sdk应用程序的前端处理的。我们有一些用户在标题(The difference between the request time and the current time is too large)中提到了这个问题,这些问题阻止了他们正确地上传。如何解决AWS错误:“请求时间和当前时间之间的差异太大”仅前端?

我知道提供的解决方案here,但我想知道是否有什么我可以做的,以确保这不会再发生任何用户,只有前端更改。有没有办法让我的请求能够正确同步?

我试图让一些用户同步他们的时钟,但它要么没有工作,要么没有正确地做。不幸的是,我不能依靠用户来解决这个问题。

+0

你可以有您的应用程序会在允许上传之前收集其当地时间并在服务器上进行检查,如果检查失败,则提醒用户并禁用“上传”功能 – Houseman

回答

1

来源: 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

除此之外,还需要添加“ D ate'到桶的CORS配置中以允许暴露日期字段。谢谢您的帮助! – G4bri3l

相关问题