I have a code snippet for generating a signed url. The below return
statement always returns empty url. Rest of the data is correctly resolved. When I debug I see that the return callback
gets executed first then the resolve
part of the function validSignedURL
gets called.
awsHelper
.s3vldSignedURL(s3Link)
.then(function(signedURL) {
data[1].url = signedURL;
return callback(null, successResponse.getResponse(context, 'OK', data));
});
The s3vldSignedURL
maps to the function below. Here s3.headobject
is promise based, used to check if a file exists in s3. I want this function to be generic, so that I can use it to generate a signed url, for any s3object.
function validSignedURL(bucket, path) {
console.log("Generating Presigned Link ... ");
const s3 = new aws.S3();
let params = {
Bucket: bucket,
Key: path
};
let checkObj = s3.getObject(params);
return new Promise(function(resolve, reject){
s3.headObject(params).promise()
.then(function (data) {
console.log('s3 File exists' + data);
resolve(getSignedURL(bucket, path));
}).catch(function (err) {
console.log('Generating Presigned Link ... Failed' + err);
resolve('');
});
});
}
The below function getSignedURL
always returns a signed url irrespective of the object exists or not.
function getSignedURL(bucket, path) {
console.log("Generating Presigned Link ... ");
const s3 = new aws.S3();
let params = {
Bucket: bucket,
Key: path
};
return s3.getSignedUrl('getObject', params);
}
Also, how can I convert the function call s3.headObject(params)
to a synchronous call which returns true or false?