UNPKG

@filesrocket/amazons3

Version:

Filerocket service to manage files from Amazon S3

68 lines 2.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseAmazonRocket = void 0; const s3_1 = __importDefault(require("aws-sdk/clients/s3")); const path_1 = require("path"); class BaseAmazonRocket { constructor(options) { this.options = options; this.s3 = new s3_1.default(options); } /** * Create a Bucket Amazon S3. Amazon S3 buckets, which are * similar to file folders, store objects, which consist * of data and its descriptive metadata. * @param name Bucket name. */ async createBucket(name) { return new Promise((resolve, reject) => { this.s3.createBucket({ Bucket: name }, (err, data) => { if (!(data === null || data === void 0 ? void 0 : data.Location) || err) return reject(err); resolve(data.Location); }); }); } paginate(data) { var _a; const items = ((_a = data.Contents) === null || _a === void 0 ? void 0 : _a.map((item) => this.builder(item, { Bucket: data.Name, Key: item.Key }))) || []; return { items, size: items.length, total: data.KeyCount, page: data.ContinuationToken, nextPage: data.NextContinuationToken || null, prevPage: null }; } /** * Generate a url for the files. * @param operation Operation. * @param params Params. */ generateUrl(operation, params) { const Bucket = this.options.Bucket; return this.s3.getSignedUrl(operation, { Bucket, ...params }); } builder(payload, query) { const { ext, base: name, dir } = (0, path_1.parse)(payload.Key); return { ...payload, id: payload.Key, name, dir, ext, size: payload.Size, url: this.generateUrl('getObject', query), updatedAt: new Date(payload.LastModified) }; } } exports.BaseAmazonRocket = BaseAmazonRocket; //# sourceMappingURL=base.js.map