UNPKG

@appotter/nestjs-s3

Version:

NestJS provider to integrates with AWS S3

154 lines 5.59 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.S3Service = void 0; const common_1 = require("@nestjs/common"); const constants_1 = require("./constants"); const helpers_1 = require("./helpers"); const uuid_1 = require("uuid"); const client_s3_1 = require("@aws-sdk/client-s3"); const lib_storage_1 = require("@aws-sdk/lib-storage"); const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner"); let S3Service = class S3Service { constructor(config) { this.config = config; const { accessKeyId, secretAccessKey, region, bucket, acl, endpoint = null, } = config; this.client = new client_s3_1.S3Client({ region, credentials: { accessKeyId, secretAccessKey }, endpoint, }); this.bucket = bucket; this.acl = acl ?? 'public-read'; } getClient() { return this.client; } async put(file, path) { path = (0, helpers_1.trimLastSlash)(path); let fileName = file.originalname; if (path !== '') { fileName = `${path}/${file.originalname}`; } const putObject = { Bucket: this.bucket, Key: (0, helpers_1.hasExtension)(path) ? path : fileName, Body: file.buffer, ContentType: file.mimetype, ACL: this.acl, }; return await this.upload(putObject); } async putAsUniqueName(file, folder) { folder = (0, helpers_1.trimLastSlash)(folder); const fileName = `${(0, uuid_1.v4)()}.${(0, helpers_1.getFileExtension)(file)}`; const putObject = { Bucket: this.bucket, Key: folder ? `${folder}/${fileName}` : fileName, Body: file.buffer, ContentType: file.mimetype, ACL: this.acl, }; return await this.upload(putObject); } async upload(putObject) { try { const upload = new lib_storage_1.Upload({ client: this.client, params: putObject, }); const uploaded = await upload.done(); return { url: uploaded.Location, origin: uploaded }; } catch (error) { common_1.Logger.error(error); throw error; } } async lists(folder) { folder = (0, helpers_1.trimLastSlash)(folder); const objectParams = { Bucket: this.bucket, Prefix: folder, }; try { const data = await this.client.send(new client_s3_1.ListObjectsCommand(objectParams)); return data.Contents?.map((item) => ({ key: item.Key, size: item.Size, lastModified: item.LastModified, bucket: this.bucket, })); } catch (error) { common_1.Logger.error(error); throw error; } } async get(key) { const objectParams = { Bucket: this.bucket, Key: key, }; try { const data = await this.client.send(new client_s3_1.GetObjectCommand(objectParams)); const stream = data.Body; const bodyBuffer = Buffer.concat(await stream.toArray()); return { key, contentLength: data.ContentLength || 0, contentType: data.ContentType || '', body: bodyBuffer, }; } catch (error) { common_1.Logger.error(error); throw error; } } async delete(key) { const objectParams = { Bucket: this.bucket, Key: key, }; try { const data = await this.client.send(new client_s3_1.DeleteObjectCommand(objectParams)); return { status: true, origin: data }; } catch (error) { common_1.Logger.error(error); throw error; } } async signedUrl(key, expiresIn) { const objectParams = { Bucket: this.bucket, Key: key, }; try { return await (0, s3_request_presigner_1.getSignedUrl)(this.client, new client_s3_1.GetObjectCommand(objectParams), { expiresIn }); } catch (error) { common_1.Logger.error(error); throw error; } } }; exports.S3Service = S3Service; exports.S3Service = S3Service = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(constants_1.S3_CONFIGURATION)), __metadata("design:paramtypes", [Object]) ], S3Service); //# sourceMappingURL=s3.service.js.map