Skip to content

Commit

Permalink
Support S3 cache provider config
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Smith committed Sep 7, 2021
1 parent f29f991 commit 9c34bcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 19 additions & 5 deletions src/providers/S3CacheProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(id: string, cacheName: CacheName): Promise<T> {
const cache = await this.getCache(cacheName);
return cache[id];
Expand All @@ -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) => {
Expand All @@ -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',
};
Expand Down

0 comments on commit 9c34bcf

Please sign in to comment.