@@ -2,11 +2,25 @@ import { CacheName } from '../models';
22import { CacheProviderInterface } from '../interfaces' ;
33const AWS = require ( 'aws-sdk' ) ;
44
5- AWS . config . update ( { region : 'us-east-1' } ) ;
5+ export type S3CacheProviderConfig = {
6+ region ?: string ,
7+ bucket : string ,
8+ keyPrefix : string ,
9+ } ;
610
711export class S3CacheProvider implements CacheProviderInterface {
12+ private config : S3CacheProviderConfig ;
813 private caches : object = { } ;
914
15+ public constructor ( config : S3CacheProviderConfig ) {
16+ this . config = config ;
17+
18+ if ( this . config . region ) {
19+ AWS . config . update ( { region : this . config . region } ) ;
20+ }
21+ }
22+
23+
1024 public async getObject < T > ( id : string , cacheName : CacheName ) : Promise < T > {
1125 const cache = await this . getCache ( cacheName ) ;
1226 return cache [ id ] ;
@@ -31,8 +45,8 @@ export class S3CacheProvider implements CacheProviderInterface {
3145 if ( ! this . caches . hasOwnProperty ( name ) ) {
3246 const s3 = new AWS . S3 ( ) ;
3347 const params = {
34- Bucket : 'prod-errorsync-redpointops' ,
35- Key : `cache/ ${ name } .json` ,
48+ Bucket : this . config . bucket ,
49+ Key : `${ this . config . keyPrefix } ${ name } .json` ,
3650 } ;
3751
3852 this . caches [ name ] = await new Promise ( ( resolve , reject ) => {
@@ -53,8 +67,8 @@ export class S3CacheProvider implements CacheProviderInterface {
5367 console . log ( 'Saving' , data ) ;
5468 const s3 = new AWS . S3 ( ) ;
5569 const params = {
56- Bucket : 'prod-errorsync-redpointops' ,
57- Key : `cache/ ${ name } .json` ,
70+ Bucket : this . config . bucket ,
71+ Key : `${ this . config . keyPrefix } ${ name } .json` ,
5872 Body : JSON . stringify ( data ) ,
5973 ContentType : 'application/json; charset=utf-8' ,
6074 } ;
0 commit comments