UNPKG

alapa

Version:

A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.

64 lines (63 loc) 2.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.S3Driver = void 0; const client_s3_1 = require("@aws-sdk/client-s3"); const fs_1 = __importDefault(require("fs")); const utils_1 = require("../utils"); const globals_1 = require("../shared/globals"); class S3Driver { error; name = "s3"; absolutePath; absoluteURL; config = { accessKeyId: process.env.AWS_ACCESS_KEY_ID || "", secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || "", endpoint: process.env.AWS_S3_ENDPOINT, bucket: process.env.AWS_S3_BUCKET || "", region: process.env.AWS_REGION || "", }; constructor(config) { this.config = config || globals_1.GlobalConfig?.storage?.s3 || this.config; const url = this.config.endpoint || `https://${config?.bucket}.s3.${config?.region}.amazonaws.com`; this.absolutePath = url; this.absoluteURL = url; } saveFile = async (filePath, key) => { utils_1.Logger.info(this.config.endpoint); // Initialize the S3 client const s3Client = new client_s3_1.S3Client({ // endpoint: this.config.endpoint, region: this.config.region, credentials: { accessKeyId: this.config.accessKeyId, secretAccessKey: this.config.secretAccessKey, }, }); try { // Read the file to be uploaded const fileStream = fs_1.default.createReadStream(filePath); // Create the command to put the object in the S3 bucket const uploadParams = { Bucket: this.config?.bucket, Key: key, // The key is the name of the file in the S3 bucket Body: fileStream, // The file's contents }; // Create the PutObjectCommand to upload the file const command = new client_s3_1.PutObjectCommand(uploadParams); // Send the command to S3 const data = await s3Client.send(command); console.log(`File uploaded successfully: ${data.ETag}`); return key; } catch (err) { utils_1.Logger.error(`Error uploading file: ${err}`); return false; } }; } exports.S3Driver = S3Driver;