feat: add s3 env variables to .env.example

This commit is contained in:
Ahmed Bouhuolia
2024-05-30 17:58:58 +02:00
parent 8ed24748ec
commit 7ff36e8c4f
5 changed files with 35 additions and 21 deletions

View File

@@ -233,10 +233,10 @@ module.exports = {
* S3 for documents.
*/
s3: {
region: process.env.AWS_REGION,
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
endpoint: process.env.AWS_ENDPOINT,
bucket: process.env.AWS_BUCKET,
region: process.env.S3_REGION,
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
endpoint: process.env.S3_ENDPOINT,
bucket: process.env.S3_BUCKET,
},
};

View File

@@ -1,13 +1,19 @@
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
import { s3 } from '@/lib/S3/S3';
import { Inject, Service } from 'typedi';
import { s3 } from '@/lib/S3/S3';
import HasTenancyService from '../Tenancy/TenancyService';
import config from '@/config';
import UnitOfWork from '../UnitOfWork';
import { Knex } from 'knex';
@Service()
export class DeleteAttachment {
@Inject()
private tenancy: HasTenancyService;
@Inject()
private uow: UnitOfWork;
/**
* Deletes the give file attachment file key.
* @param {number} tenantId
@@ -17,7 +23,7 @@ export class DeleteAttachment {
const { Document, DocumentLink } = this.tenancy.models(tenantId);
const params = {
Bucket: process.env.AWS_BUCKET,
Bucket: config.s3.bucket,
Key: filekey,
};
await s3.send(new DeleteObjectCommand(params));
@@ -26,10 +32,14 @@ export class DeleteAttachment {
.findOne('key', filekey)
.throwIfNotFound();
// Delete all document links
await DocumentLink.query().where('documentId', foundDocument.id).delete();
await this.uow.withTransaction(tenantId, async (trx: Knex.Transaction) => {
// Delete all document links
await DocumentLink.query(trx)
.where('documentId', foundDocument.id)
.delete();
// Delete thedocument.
await Document.query().findById(foundDocument.id).delete();
// Delete thedocument.
await Document.query(trx).findById(foundDocument.id).delete();
});
}
}

View File

@@ -1,6 +1,7 @@
import { Service } from 'typedi';
import { s3 } from '@/lib/S3/S3';
import { GetObjectCommand } from '@aws-sdk/client-s3';
import { s3 } from '@/lib/S3/S3';
import config from '@/config';
@Service()
export class GetAttachment {
@@ -11,7 +12,7 @@ export class GetAttachment {
*/
async getAttachment(tenantId: number, filekey: string) {
const params = {
Bucket: process.env.AWS_BUCKET,
Bucket: config.s3.bucket,
Key: filekey,
};
const data = await s3.send(new GetObjectCommand(params));

View File

@@ -1,23 +1,19 @@
import { Service } from 'typedi';
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';
import config from '@/config';
@Service()
export class getAttachmentPresignedUrl {
/**
* Retrieves the presigned url of the given attachment key.
* @param {string} 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,
Bucket: config.s3.bucket,
Key: key,
});
const signedUrl = await getSignedUrl(s3, command, { expiresIn: 300 });