feat: getting presigned url of the uploaded attachment

This commit is contained in:
Ahmed Bouhuolia
2024-05-29 16:16:08 +02:00
parent e7871e34a9
commit ceb133e29a
7 changed files with 124 additions and 17 deletions

View File

@@ -0,0 +1,27 @@
import { GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { s3 } from '@/lib/S3/S3';
import { Service } from 'typedi';
@Service()
export class getAttachmentPresignedUrl {
/**
* Retrieves the presigned url of the given attachment key.
* @param {string} key
* @returns {Promise<string?>}
*/
async getPresignedUrl(key: string) {
const params = {
Bucket: process.env.AWS_BUCKET,
Key: key,
Expires: 60 * 5, // 5 minutes
};
const command = new GetObjectCommand({
Bucket: process.env.AWS_BUCKET,
Key: key,
});
const signedUrl = await getSignedUrl(s3, command, { expiresIn: 300 });
return signedUrl;
}
}