Skip to content

Commit

Permalink
feat: add aws s3 provider
Browse files Browse the repository at this point in the history
  • Loading branch information
socaseinpoint committed May 26, 2021
1 parent 3966e90 commit 773348a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/providers/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const AbstractFileTransfer = require('ms-files-transport');
const S3 = require('aws-sdk/clients/s3');
const { merge } = require('lodash');
const assert = require('assert');

const UPLOAD_URL_EXPIRES_IN_SEC = 60000;
const DOWNLOAD_URL_EXPIRES_IN_SEC = 60000;

class AWSTransport extends AbstractFileTransfer {
constructor(opts = {}) {
super();

const config = merge({}, AWSTransport.defaultOpts, opts);
this._config = config;
this._logger = this._config.logger;

assert(config !== undefined);
assert(config.options !== undefined);
assert(config.options.accessKeyId !== undefined);
assert(config.options.secretAccessKey !== undefined);
assert(config.options.region !== undefined);
assert(config.options.bucketName !== undefined);

this.setupS3();
}

setupS3() {
try {
this._s3 = new S3({
signatureVersion: 'v4',
region: this._config.region,
credentials: {
accessKeyId: this._config.accessKeyId,
secretAccessKey: this._config.secretAccessKey,
},
});
} catch (e) {
this._logger.warn('failed to load aws-sdk/clients/s3', e);
}
}

connect() {
return Promise
.bind(this)
.then(this.findOrCreateBucket);
}

async findOrCreateBucket() {
const { bucketName } = this._config;

const s3 = this._s3;

const buckets = s3.listBuckets();
const exists = buckets.find(bucketName);

if (!exists) {
this.log.debug('initiating createBucket: %s', bucketName);
await this._s3.createBucket({
Bucket: bucketName,
});
}
}

async getSignedUrl(action, input) {
const params = {
Bucket: this._config.bucketName,
Expires: action === 'putObject' ? UPLOAD_URL_EXPIRES_IN_SEC : DOWNLOAD_URL_EXPIRES_IN_SEC,
Key: input.key,
};

if (action === 'putObject') {
params.ContentType = input.contentType;
}

return new Promise((resolve, reject) => {
this._s3.getSignedUrl('putObject', params, (err, url) => {
if (err) {
return reject(err);
}
return resolve(url);
});
});
}
}

AWSTransport.defaultOpts = {};

module.exports = AWSTransport;
8 changes: 8 additions & 0 deletions src/providers/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class ProviderFactory {

return provider;
}

// eslint-disable-next-line class-methods-use-this
getProviderAWS(transport) {
const ProviderAWS = require('./aws');
const provider = new ProviderAWS(transport);

return provider;
}
}

module.exports = ProviderFactory;
6 changes: 6 additions & 0 deletions src/providers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ function initProviders(service) {
factory.getProviderOSS(transport)
);
}

if (transport.name === 'aws') {
service.providers.push(
factory.getProviderAWS(transport)
);
}
}

// create providerByBucket map for fast access
Expand Down
18 changes: 18 additions & 0 deletions test/suites/providers/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// const { strictEqual } = require('assert');
const AWSTransport = require('../../../src/providers/aws');

describe('util fetch-data suite', () => {
it('should be able to create instance', () => {
const provider = new AWSTransport({
options: {
accessKeyId: 'AKIASS5V3WV23VA4KYOF',
accessKeySecret: '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',
bucket: 'test',
region: 'us-west-2',
secure: true,
},
});

console.log(provider);
});
});

0 comments on commit 773348a

Please sign in to comment.