Last active
September 28, 2018 20:28
-
-
Save rhythnic/525c8cdfdfebbae1ed47646d71d86102 to your computer and use it in GitHub Desktop.
Implementation of Directory class for s3
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
const { Directory } = require('./Directory') | |
const AWS = require('aws-sdk') | |
const { PassThrough } = require('stream') | |
const s3 = new AWS.S3({ apiVersion: '2006-03-01' }) | |
module.exports = class S3Directory extends Directory { | |
constructor (opts) { | |
super(opts) | |
this.Bucket = opts.Bucket | |
} | |
createReadStream (path) { | |
const opts = { Bucket: this.Bucket, Key: this.getFullPath(path) } | |
return s3.getObject(opts).createReadStream() | |
} | |
createWriteStream (path) { | |
const Body = new PassThrough() | |
const Key = this.getFullPath(path) | |
s3.upload({ Bucket: this.Bucket, Key, Body }, err => { | |
if (err) Body.destroy(err) | |
}) | |
return Body | |
} | |
async readFile (path) { | |
const opts = { Bucket: this.Bucket, Key: this.getFullPath(path) } | |
const response = await s3.getObject(opts).promise() | |
return response.Body | |
} | |
writeFile (path, Body) { | |
const Key = this.getFullPath(path) | |
return s3.upload({ Bucket: this.Bucket, Key, Body }).promise() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment