2017-04-26 35 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

相關問題