Skip to content

Commit 9c34bcf

Browse files
author
Nicholas Smith
committed
Support S3 cache provider config
1 parent f29f991 commit 9c34bcf

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "error-sync-lib",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"private": "true",
55
"scripts": {
66
"build": "yarn build:esm && yarn build:cjs",

src/providers/S3CacheProvider.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,25 @@ import { CacheName } from '../models';
22
import { CacheProviderInterface } from '../interfaces';
33
const 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

711
export 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

Comments
 (0)