-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3966e90
commit 773348a
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |