diff --git a/package.json b/package.json index 4fd3671..791548a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "error-sync-lib", - "version": "0.1.2", + "version": "0.1.3", "private": "true", "scripts": { "build": "yarn build:esm && yarn build:cjs", diff --git a/src/providers/S3CacheProvider.ts b/src/providers/S3CacheProvider.ts index e4887c5..ff3a274 100644 --- a/src/providers/S3CacheProvider.ts +++ b/src/providers/S3CacheProvider.ts @@ -2,11 +2,25 @@ import { CacheName } from '../models'; import { CacheProviderInterface } from '../interfaces'; const AWS = require('aws-sdk'); -AWS.config.update({region: 'us-east-1'}); +export type S3CacheProviderConfig = { + region?: string, + bucket: string, + keyPrefix: string, +}; export class S3CacheProvider implements CacheProviderInterface { + private config: S3CacheProviderConfig; private caches: object = {}; + public constructor(config: S3CacheProviderConfig) { + this.config = config; + + if (this.config.region) { + AWS.config.update({ region: this.config.region }); + } + } + + public async getObject(id: string, cacheName: CacheName): Promise { const cache = await this.getCache(cacheName); return cache[id]; @@ -31,8 +45,8 @@ export class S3CacheProvider implements CacheProviderInterface { if (!this.caches.hasOwnProperty(name)) { const s3 = new AWS.S3(); const params = { - Bucket: 'prod-errorsync-redpointops', - Key: `cache/${name}.json`, + Bucket: this.config.bucket, + Key: `${this.config.keyPrefix}${name}.json`, }; this.caches[name] = await new Promise((resolve, reject) => { @@ -53,8 +67,8 @@ export class S3CacheProvider implements CacheProviderInterface { console.log('Saving', data); const s3 = new AWS.S3(); const params = { - Bucket: 'prod-errorsync-redpointops', - Key: `cache/${name}.json`, + Bucket: this.config.bucket, + Key: `${this.config.keyPrefix}${name}.json`, Body: JSON.stringify(data), ContentType: 'application/json; charset=utf-8', };