|
| 1 | +// S3Adapter |
| 2 | +// |
| 3 | +// Stores Parse files in AWS S3. |
| 4 | + |
| 5 | +var AWS = require('aws-sdk'); |
| 6 | +var path = require('path'); |
| 7 | + |
| 8 | +var DEFAULT_REGION = "us-east-1"; |
| 9 | +var DEFAULT_BUCKET = "parse-files"; |
| 10 | + |
| 11 | +// Creates an S3 session. |
| 12 | +// Providing AWS access and secret keys is mandatory |
| 13 | +// Region and bucket will use sane defaults if omitted |
| 14 | +function S3Adapter(accessKey, secretKey, options) { |
| 15 | + options = options || {}; |
| 16 | + |
| 17 | + this.region = options.region || DEFAULT_REGION; |
| 18 | + this.bucket = options.bucket || DEFAULT_BUCKET; |
| 19 | + this.bucketPrefix = options.bucketPrefix || ""; |
| 20 | + this.directAccess = options.directAccess || false; |
| 21 | + |
| 22 | + s3Options = { |
| 23 | + accessKeyId: accessKey, |
| 24 | + secretAccessKey: secretKey, |
| 25 | + params: {Bucket: this.bucket} |
| 26 | + }; |
| 27 | + AWS.config.region = this.region; |
| 28 | + this.s3 = new AWS.S3(s3Options); |
| 29 | +} |
| 30 | + |
| 31 | +// For a given config object, filename, and data, store a file in S3 |
| 32 | +// Returns a promise containing the S3 object creation response |
| 33 | +S3Adapter.prototype.create = function(config, filename, data) { |
| 34 | + var params = { |
| 35 | + Key: this.bucketPrefix + filename, |
| 36 | + Body: data, |
| 37 | + }; |
| 38 | + if (this.directAccess) { |
| 39 | + params.ACL = "public-read" |
| 40 | + } |
| 41 | + |
| 42 | + return new Promise((resolve, reject) => { |
| 43 | + this.s3.upload(params, function(err, data) { |
| 44 | + if (err !== null) return reject(err); |
| 45 | + resolve(data); |
| 46 | + }); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +// Search for and return a file if found by filename |
| 51 | +// Returns a promise that succeeds with the buffer result from S3 |
| 52 | +S3Adapter.prototype.get = function(config, filename) { |
| 53 | + var params = {Key: this.bucketPrefix + filename}; |
| 54 | + |
| 55 | + return new Promise((resolve, reject) => { |
| 56 | + this.s3.getObject(params, (err, data) => { |
| 57 | + if (err !== null) return reject(err); |
| 58 | + resolve(data.Body); |
| 59 | + }); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +// Generates and returns the location of a file stored in S3 for the given request and |
| 64 | +// filename |
| 65 | +// The location is the direct S3 link if the option is set, otherwise we serve |
| 66 | +// the file through parse-server |
| 67 | +S3Adapter.prototype.location = function(config, req, filename) { |
| 68 | + if (this.directAccess) { |
| 69 | + return ('https://' + this.bucket + '.s3.amazonaws.com' + '/' + |
| 70 | + this.bucketPrefix + filename); |
| 71 | + } |
| 72 | + return (req.protocol + '://' + req.get('host') + |
| 73 | + path.dirname(req.originalUrl) + '/' + req.config.applicationId + |
| 74 | + '/' + encodeURIComponent(filename)); |
| 75 | +} |
| 76 | + |
| 77 | +module.exports = S3Adapter; |
0 commit comments