Merge pull request #1416 from factorysh/s3_endpoint
Choose your endpoint.
This commit is contained in:
commit
17afd6ad58
3 changed files with 25 additions and 6 deletions
|
@ -9,6 +9,16 @@ const conf = convict({
|
||||||
default: '',
|
default: '',
|
||||||
env: 'S3_BUCKET'
|
env: 'S3_BUCKET'
|
||||||
},
|
},
|
||||||
|
s3_endpoint: {
|
||||||
|
format: String,
|
||||||
|
default: '',
|
||||||
|
env: 'S3_ENDPOINT'
|
||||||
|
},
|
||||||
|
s3_use_path_style_endpoint: {
|
||||||
|
format: Boolean,
|
||||||
|
default: false,
|
||||||
|
env: 'S3_USE_PATH_STYLE_ENDPOINT'
|
||||||
|
},
|
||||||
gcs_bucket: {
|
gcs_bucket: {
|
||||||
format: String,
|
format: String,
|
||||||
default: '',
|
default: '',
|
||||||
|
|
|
@ -1,25 +1,31 @@
|
||||||
const AWS = require('aws-sdk');
|
const AWS = require('aws-sdk');
|
||||||
const s3 = new AWS.S3();
|
|
||||||
|
|
||||||
class S3Storage {
|
class S3Storage {
|
||||||
constructor(config, log) {
|
constructor(config, log) {
|
||||||
this.bucket = config.s3_bucket;
|
this.bucket = config.s3_bucket;
|
||||||
this.log = log;
|
this.log = log;
|
||||||
|
const cfg = {};
|
||||||
|
if (config.s3_endpoint != '') {
|
||||||
|
cfg['endpoint'] = config.s3_endpoint;
|
||||||
|
}
|
||||||
|
cfg['s3ForcePathStyle'] = config.s3_use_path_style_endpoint
|
||||||
|
AWS.config.update(cfg);
|
||||||
|
this.s3 = new AWS.S3();
|
||||||
}
|
}
|
||||||
|
|
||||||
async length(id) {
|
async length(id) {
|
||||||
const result = await s3
|
const result = await this.s3
|
||||||
.headObject({ Bucket: this.bucket, Key: id })
|
.headObject({ Bucket: this.bucket, Key: id })
|
||||||
.promise();
|
.promise();
|
||||||
return result.ContentLength;
|
return result.ContentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
getStream(id) {
|
getStream(id) {
|
||||||
return s3.getObject({ Bucket: this.bucket, Key: id }).createReadStream();
|
return this.s3.getObject({ Bucket: this.bucket, Key: id }).createReadStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
set(id, file) {
|
set(id, file) {
|
||||||
const upload = s3.upload({
|
const upload = this.s3.upload({
|
||||||
Bucket: this.bucket,
|
Bucket: this.bucket,
|
||||||
Key: id,
|
Key: id,
|
||||||
Body: file
|
Body: file
|
||||||
|
@ -29,11 +35,11 @@ class S3Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
del(id) {
|
del(id) {
|
||||||
return s3.deleteObject({ Bucket: this.bucket, Key: id }).promise();
|
return this.s3.deleteObject({ Bucket: this.bucket, Key: id }).promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
ping() {
|
ping() {
|
||||||
return s3.headBucket({ Bucket: this.bucket }).promise();
|
return this.s3.headBucket({ Bucket: this.bucket }).promise();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,9 @@ const s3Stub = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const awsStub = {
|
const awsStub = {
|
||||||
|
config: {
|
||||||
|
update: sinon.stub()
|
||||||
|
},
|
||||||
S3: function() {
|
S3: function() {
|
||||||
return s3Stub;
|
return s3Stub;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue