I have a list called students.Assume it contains 100 items.So every students pro picture is stored in s3 bucket with the student id as it's file name.
My question is how to access all these images with presigned urls.
If I generate url when getting data, I think it's a bad practice,because it's getting some time to finish in server side. I can't store it.It will expire and I saw in many articles saying it's a bad practice. How to do this.Is there a way to do this in client side?.I'm using Angular 8 in client side.
Here is the code I use to generate pre-signed url.This code is working fine.But the problem is after the url expire, again needs to generate it.
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const s3BucketName = 'media';
exports.handler = async (event) => {
try{
const objectKey = '1234.jpg';
let params = {
Bucket: s3BucketName,
Key: objectKey,
Expires: 60 * 60 * 24 * 365 * 10
};
return await s3.getSignedUrlPromise('getObject', params).then(async url => {
console.log('Successfully generated pre signed image url:', url);
const payload = {
message: 'success',
imageUrl: url
};
const response = {
statusCode: 201,
body: JSON.stringify(payload),
};
return response;
}).catch(error => {
console.error(`Failed to generate presigned url: ${error}`);
return {
statusCode: error.statusCode || 501,
body: JSON.stringify({
message: `Failed to generate presigned url. Request Id: ${context.awsRequestId}`
})
};
});
}catch(error){
console.log(JSON.stringify(error));
const response = {
statusCode: 501,
body: JSON.stringify(error),
};
return response;
}
};
please help me. thanks