import BaseService from "@Services/BaseService"; import { Service } from "typedi"; import * as AWS from "aws-sdk"; import { BackendVariables } from "@Common/config/variables/Variables"; import path from "path"; @Service() export default class OfficerRibService extends BaseService { private readonly s3: AWS.S3; constructor(private variables: BackendVariables) { super(); // Configure the AWS SDK for Scaleway this.s3 = new AWS.S3({ accessKeyId: this.variables.SCW_ACCESS_KEY_ID, secretAccessKey: this.variables.SCW_ACCESS_KEY_SECRET, endpoint: this.variables.SCW_BUCKET_ENDPOINT, // Use the appropriate Scaleway endpoint s3ForcePathStyle: true, // Needed for Scaleway's S3-compatible API signatureVersion: "v4", }); } public async getByUid(uid: string, fileName: string) { const key = path.join(this.variables.ENV, uid, fileName); return new Promise(async (resolve, reject) => { this.s3.getObject( { Bucket: this.variables.SCW_BUCKET_NAME, Key: key, }, function (err, data) { if (err) return reject(err); resolve(data); }, ); }); } public async createOrUpdate(officeId: string, file: Express.Multer.File) { const key = path.join(this.variables.ENV, officeId, file.originalname); const uploadParams = { Bucket: this.variables.SCW_BUCKET_NAME, Key: key, // Example: 'example.txt' Body: file.buffer, // Example: fs.createReadStream('/path/to/file') ACL: "public-read", // Optional: Set the ACL if needed }; return new Promise((resolve, reject) => { this.s3.putObject(uploadParams, function (err, data) { if (err) return reject(err); resolve(`https://lecoffre-bucket.s3.fr-par.scw.cloud/lecoffre-bucket/${key}`); }); }); } public async delete(officeId: string, fileName: string) { const key = path.join(this.variables.ENV, officeId, fileName); return new Promise(async (resolve, reject) => { this.s3.getObject( { Bucket: this.variables.SCW_BUCKET_NAME, Key: key, }, function (err, data) { if (err) return reject(err); resolve(data); }, ); }); } }