UNPKG

@studyportals/sp-r2d2

Version:

A framework that contains various components used when developing projects that will be deployed via AWS λ.

88 lines 3.52 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.S3Adapter = void 0; const client_s3_1 = require("@aws-sdk/client-s3"); class S3Adapter { get bucketName() { return this._bucketName; } get useAccelerateEndpoint() { return this._useAccelerateEndpoint; } get client() { if (!this._client) { this._client = new client_s3_1.S3Client({ useAccelerateEndpoint: this.useAccelerateEndpoint }); } return this._client; } constructor(bucketName = '', useAccelerateEndpoint = false) { this._bucketName = bucketName; this._useAccelerateEndpoint = useAccelerateEndpoint; } getJSON(key, enforceContentType = true) { return __awaiter(this, void 0, void 0, function* () { const command = new client_s3_1.GetObjectCommand({ Bucket: this.bucketName, Key: key, }); try { const response = yield this.client.send(command); if (enforceContentType && 'application/json' !== response.ContentType) { throw new Error('The received data is not JSON'); } return this.transformToString(response.Body); } catch (err) { console.error(`An error occurred when attempting to get object from ${this.bucketName}: `, err); throw err; } }); } putJSON(key, json) { return __awaiter(this, void 0, void 0, function* () { const command = new client_s3_1.PutObjectCommand({ Bucket: this.bucketName, Key: key, Body: json, ContentType: 'application/json', }); try { yield this.client.send(command); } catch (err) { console.error(`An error occurred when attempting to put object in ${this.bucketName}: `, err); throw err; } }); } delete(key) { return __awaiter(this, void 0, void 0, function* () { const command = new client_s3_1.DeleteObjectCommand({ Bucket: this.bucketName, Key: key, }); try { yield this.client.send(command); } catch (err) { console.error(`An error occurred when attempting to delete object from ${this.bucketName}: `, err); throw err; } }); } transformToString(body) { var _a; return (_a = body === null || body === void 0 ? void 0 : body.transformToString()) !== null && _a !== void 0 ? _a : '{}'; } } exports.S3Adapter = S3Adapter; //# sourceMappingURL=s3-adapter.class.js.map