Skip to content

Instantly share code, notes, and snippets.

@tommedema
Last active March 31, 2020 23:00
Show Gist options
  • Save tommedema/13b5840ac3b1003178dfab8a0aa6bc6d to your computer and use it in GitHub Desktop.
Save tommedema/13b5840ac3b1003178dfab8a0aa6bc6d to your computer and use it in GitHub Desktop.
/**
* Note that this temporarily works around an issue where Amplify does not support
* the AWS SDK' correctClockSkew option, by retrying with a manually defined offset
* if the request time is too skewed.
* @see https://github.com/aws-amplify/amplify-js/issues/2014
*/
const attemptPutWithClockSkewRetry = () =>
Storage
.put(
itemKey,
itemData
)
.catch(async err => {
if (err.code === 'RequestTimeTooSkewed') {
const serverTimeFetch = await fetch('https://your-lambda-function-returning-server-time.amazonaws.com/time', { headers: { "Accept": "application/json" } })
const serverTimeFetchResponse = await serverTimeFetch.json()
const systemClockOffset = serverTimeFetchResponse.time - Date.now()
console.log(`setting systemClockOffset to %s`, systemClockOffset)
AWS.config.update({
systemClockOffset
})
if (!err.retryable) {
attemptPutWithClockSkewRetry()
}
}
else {
throw err
}
})
attemptPutWithClockSkewRetry()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment