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

@@ -103,3 +103,10 @@ PLAID_DEVELOPMENT_REDIRECT_URI=
LEMONSQUEEZY_API_KEY= LEMONSQUEEZY_API_KEY=
LEMONSQUEEZY_STORE_ID= LEMONSQUEEZY_STORE_ID=
LEMONSQUEEZY_WEBHOOK_SECRET= LEMONSQUEEZY_WEBHOOK_SECRET=
# S3 documents and attachments
S3_REGION=
S3_ACCESS_KEY_ID=
S3_SECRET_ACCESS_KEY=
S3_ENDPOINT=
S3_BUCKET=

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,8 @@
import { Service } from 'typedi';
import { GetObjectCommand } from '@aws-sdk/client-s3'; import { GetObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'; import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import { s3 } from '@/lib/S3/S3'; import { s3 } from '@/lib/S3/S3';
import { Service } from 'typedi'; import config from '@/config';
@Service() @Service()
export class getAttachmentPresignedUrl { export class getAttachmentPresignedUrl {
@@ -11,13 +12,8 @@ export class getAttachmentPresignedUrl {
* @returns {Promise<string?>} * @returns {Promise<string?>}
*/ */
async getPresignedUrl(key: string) { async getPresignedUrl(key: string) {
const params = {
Bucket: process.env.AWS_BUCKET,
Key: key,
Expires: 60 * 5, // 5 minutes
};
const command = new GetObjectCommand({ const command = new GetObjectCommand({
Bucket: process.env.AWS_BUCKET, Bucket: config.s3.bucket,
Key: key, Key: key,
}); });
const signedUrl = await getSignedUrl(s3, command, { expiresIn: 300 }); const signedUrl = await getSignedUrl(s3, command, { expiresIn: 300 });