UNPKG

@churchapps/apihelper

Version:

Library of helper functions not specific to any one ChurchApps project or framework.

120 lines 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AwsHelper = void 0; const client_s3_1 = require("@aws-sdk/client-s3"); const s3_presigned_post_1 = require("@aws-sdk/s3-presigned-post"); const _1 = require("."); const client_ssm_1 = require("@aws-sdk/client-ssm"); class AwsHelper { //Pulls from AWS SSM Parameter Store static async readParameter(parameterName) { let result = ""; try { const ssm = new client_ssm_1.SSMClient({ region: "us-east-2" }); const params = { Name: parameterName, WithDecryption: true }; const command = new client_ssm_1.GetParameterCommand(params); const response = await ssm.send(command); result = response?.Parameter?.Value || ""; } catch { result = ""; } return result; } static getClient() { if (!this._client) { this._client = new client_s3_1.S3Client({}); } return this._client; } static async S3PresignedUrl(key) { if (key.startsWith("/")) key = key.substring(1); const { url, fields } = await (0, s3_presigned_post_1.createPresignedPost)(this.getClient(), { Bucket: _1.EnvironmentBase.s3Bucket, Key: key, Conditions: [ ["starts-with", "$Content-Type", ""], { acl: "public-read" } ], Expires: 3600 // 1 hour }); return { url, fields, key }; } static async S3Upload(key, contentType, contents) { if (key.startsWith("/")) key = key.substring(1); const command = new client_s3_1.PutObjectCommand({ Bucket: _1.EnvironmentBase.s3Bucket, Key: key, Body: contents, ACL: "public-read", ContentType: contentType }); await this.getClient().send(command); } static async S3Remove(key) { if (key.startsWith("/")) key = key.substring(1); const command = new client_s3_1.DeleteObjectCommand({ Bucket: _1.EnvironmentBase.s3Bucket, Key: key }); await this.getClient().send(command); } static async S3Rename(oldKey, newKey) { console.log(`Renaming: ${oldKey} to ${newKey}`); await this.S3Copy(oldKey, newKey); await this.S3Remove(oldKey); } static S3Move(oldKey, newKey) { return this.S3Rename(oldKey, newKey); } static async S3Copy(oldKey, newKey) { const command = new client_s3_1.CopyObjectCommand({ Bucket: _1.EnvironmentBase.s3Bucket, CopySource: `/${_1.EnvironmentBase.s3Bucket}/${oldKey}`, Key: newKey, ACL: "public-read" }); await this.getClient().send(command); } static async S3List(path) { return this.S3ListMultiPage(this.getClient(), _1.EnvironmentBase.s3Bucket, path); } static async S3ListMultiPage(s3, bucket, path) { const result = []; let continuationToken; do { const { Contents, NextContinuationToken } = await this.S3ListManual(s3, bucket, path, continuationToken); result.push(...(Contents?.map((item) => item.Key).filter((key) => key !== undefined) || [])); continuationToken = NextContinuationToken; } while (continuationToken); return result; } static async S3ListManual(s3, bucket, path, continuationToken) { const command = new client_s3_1.ListObjectsV2Command({ Bucket: bucket, Prefix: path, MaxKeys: 10000, ContinuationToken: continuationToken }); return s3.send(command); } static async S3Read(key) { try { const command = new client_s3_1.GetObjectCommand({ Bucket: _1.EnvironmentBase.s3Bucket, Key: key }); const response = await this.getClient().send(command); return await response.Body?.transformToString(); } catch (error) { console.error("Error reading from S3:", error); return null; } } } exports.AwsHelper = AwsHelper; //# sourceMappingURL=AwsHelper.js.map