Skip to content

Commit

Permalink
Merge pull request #3115 from uselagoon/s3link-check-fix
Browse files Browse the repository at this point in the history
Fix the s3 restore check
  • Loading branch information
tobybellwood authored Apr 18, 2022
2 parents a74d4e5 + d205520 commit 4eb5824
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions services/api/src/resources/backup/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,36 @@ export const getRestoreLocation: ResolverFn = async (


// before generating the signed url, check the object exists
await s3Client.headObject({
let exists = false
const restoreLoc = await s3Client.headObject({
Bucket: R.prop(2, s3Parts),
Key: R.prop(3, s3Parts)
}, async function(err, data) {
if (err) {
// if there is an error, then delete the restore from the database
await query(
sqlClientPool,
Sql.deleteRestore({
backupId
})
);
return ""
} else {
// otherwise return the signed url
return s3Client.getSignedUrl('getObject', {
Bucket: R.prop(2, s3Parts),
Key: R.prop(3, s3Parts),
Expires: 300 // 5 minutes
});
}
});
try {
await Promise.all([restoreLoc.promise()]).then(data => {
// the file exists
}).catch(err => {
if (err) throw err;
});
exists = true
} catch(err) {
exists = false
}
if (exists) {
return s3Client.getSignedUrl('getObject', {
Bucket: R.prop(2, s3Parts),
Key: R.prop(3, s3Parts),
Expires: 300 // 5 minutes
});
} else {
await query(
sqlClientPool,
Sql.deleteRestore({
backupId
})
);
return ""
}
}

return restoreLocation;
Expand Down

0 comments on commit 4eb5824

Please sign in to comment.