1- 'use strict' ;
21// S3Adapter
32//
43// Stores Parse files in AWS S3.
54
6- var AWS = require ( 'aws-sdk' ) ;
7- var optionsFromArguments = require ( './lib/optionsFromArguments' ) ;
5+ const AWS = require ( 'aws-sdk' ) ;
6+ const optionsFromArguments = require ( './lib/optionsFromArguments' ) ;
87
98const awsCredentialsDeprecationNotice = function awsCredentialsDeprecationNotice ( ) {
109 // eslint-disable-next-line no-console
1110 console . warn ( 'Passing AWS credentials to this adapter is now DEPRECATED and will be removed in a future version' ,
1211 'See: https://github.com/parse-server-modules/parse-server-s3-adapter#aws-credentials for details' ) ;
13- }
12+ } ;
1413
1514// Creates an S3 session.
1615// Providing AWS access, secret keys and bucket are mandatory
1716// Region will use sane defaults if omitted
18- function S3Adapter ( ) {
19- var options = optionsFromArguments ( arguments ) ;
17+ function S3Adapter ( ... args ) {
18+ const options = optionsFromArguments ( args ) ;
2019 this . _region = options . region ;
2120 this . _bucket = options . bucket ;
2221 this . _bucketPrefix = options . bucketPrefix ;
@@ -27,11 +26,11 @@ function S3Adapter() {
2726 this . _globalCacheControl = options . globalCacheControl ;
2827 this . _encryption = options . ServerSideEncryption ;
2928
30- let s3Options = {
29+ const s3Options = {
3130 params : { Bucket : this . _bucket } ,
3231 region : this . _region ,
3332 signatureVersion : this . _signatureVersion ,
34- globalCacheControl : this . _globalCacheControl
33+ globalCacheControl : this . _globalCacheControl ,
3534 } ;
3635
3736 if ( options . accessKey && options . secretKey ) {
@@ -46,8 +45,8 @@ function S3Adapter() {
4645 this . _hasBucket = false ;
4746}
4847
49- S3Adapter . prototype . createBucket = function ( ) {
50- var promise ;
48+ S3Adapter . prototype . createBucket = function ( ) {
49+ let promise ;
5150 if ( this . _hasBucket ) {
5251 promise = Promise . resolve ( ) ;
5352 } else {
@@ -59,118 +58,110 @@ S3Adapter.prototype.createBucket = function() {
5958 } ) ;
6059 }
6160 return promise ;
62- }
61+ } ;
6362
6463// For a given config object, filename, and data, store a file in S3
6564// Returns a promise containing the S3 object creation response
66- S3Adapter . prototype . createFile = function ( filename , data , contentType ) {
67- let params = {
65+ S3Adapter . prototype . createFile = function ( filename , data , contentType ) {
66+ const params = {
6867 Key : this . _bucketPrefix + filename ,
69- Body : data
68+ Body : data ,
7069 } ;
7170 if ( this . _directAccess ) {
72- params . ACL = " public-read"
71+ params . ACL = ' public-read' ;
7372 }
7473 if ( contentType ) {
7574 params . ContentType = contentType ;
7675 }
77- if ( this . _globalCacheControl ) {
76+ if ( this . _globalCacheControl ) {
7877 params . CacheControl = this . _globalCacheControl ;
7978 }
80- if ( this . _encryption == 'AES256' || this . _encryption == 'aws:kms' ) {
79+ if ( this . _encryption === 'AES256' || this . _encryption === 'aws:kms' ) {
8180 params . ServerSideEncryption = this . _encryption ;
8281 }
83- return this . createBucket ( ) . then ( ( ) => {
84- return new Promise ( ( resolve , reject ) => {
85- this . _s3Client . upload ( params , ( err , data ) => {
86- if ( err !== null ) {
87- return reject ( err ) ;
88- }
89- resolve ( data ) ;
90- } ) ;
82+ return this . createBucket ( ) . then ( ( ) => new Promise ( ( resolve , reject ) => {
83+ this . _s3Client . upload ( params , ( err , response ) => {
84+ if ( err !== null ) {
85+ return reject ( err ) ;
86+ }
87+ return resolve ( response ) ;
9188 } ) ;
92- } ) ;
93- }
89+ } ) ) ;
90+ } ;
9491
95- S3Adapter . prototype . deleteFile = function ( filename ) {
96- return this . createBucket ( ) . then ( ( ) => {
97- return new Promise ( ( resolve , reject ) => {
98- let params = {
99- Key : this . _bucketPrefix + filename
100- } ;
101- this . _s3Client . deleteObject ( params , ( err , data ) => {
102- if ( err !== null ) {
103- return reject ( err ) ;
104- }
105- resolve ( data ) ;
106- } ) ;
92+ S3Adapter . prototype . deleteFile = function ( filename ) {
93+ return this . createBucket ( ) . then ( ( ) => new Promise ( ( resolve , reject ) => {
94+ const params = {
95+ Key : this . _bucketPrefix + filename ,
96+ } ;
97+ this . _s3Client . deleteObject ( params , ( err , data ) => {
98+ if ( err !== null ) {
99+ return reject ( err ) ;
100+ }
101+ return resolve ( data ) ;
107102 } ) ;
108- } ) ;
109- }
103+ } ) ) ;
104+ } ;
110105
111106// Search for and return a file if found by filename
112107// Returns a promise that succeeds with the buffer result from S3
113- S3Adapter . prototype . getFileData = function ( filename ) {
114- let params = { Key : this . _bucketPrefix + filename } ;
115- return this . createBucket ( ) . then ( ( ) => {
116- return new Promise ( ( resolve , reject ) => {
117- this . _s3Client . getObject ( params , ( err , data ) => {
118- if ( err !== null ) {
119- return reject ( err ) ;
120- }
121- // Something happened here...
122- if ( data && ! data . Body ) {
123- return reject ( data ) ;
124- }
125- resolve ( data . Body ) ;
126- } ) ;
108+ S3Adapter . prototype . getFileData = function ( filename ) {
109+ const params = { Key : this . _bucketPrefix + filename } ;
110+ return this . createBucket ( ) . then ( ( ) => new Promise ( ( resolve , reject ) => {
111+ this . _s3Client . getObject ( params , ( err , data ) => {
112+ if ( err !== null ) {
113+ return reject ( err ) ;
114+ }
115+ // Something happened here...
116+ if ( data && ! data . Body ) {
117+ return reject ( data ) ;
118+ }
119+ return resolve ( data . Body ) ;
127120 } ) ;
128- } ) ;
129- }
121+ } ) ) ;
122+ } ;
130123
131124// Generates and returns the location of a file stored in S3 for the given request and filename
132- // The location is the direct S3 link if the option is set, otherwise we serve the file through parse-server
133- S3Adapter . prototype . getFileLocation = function ( config , filename ) {
134- filename = encodeURIComponent ( filename ) ;
125+ // The location is the direct S3 link if the option is set,
126+ // otherwise we serve the file through parse-server
127+ S3Adapter . prototype . getFileLocation = function ( config , filename ) {
128+ const fileName = encodeURIComponent ( filename ) ;
135129 if ( this . _directAccess ) {
136130 if ( this . _baseUrl && this . _baseUrlDirect ) {
137- return `${ this . _baseUrl } /${ filename } ` ;
138- } else if ( this . _baseUrl ) {
139- return `${ this . _baseUrl } /${ this . _bucketPrefix + filename } ` ;
140- } else {
141- return `https://${ this . _bucket } .s3.amazonaws.com/${ this . _bucketPrefix + filename } ` ;
131+ return `${ this . _baseUrl } /${ fileName } ` ;
132+ } if ( this . _baseUrl ) {
133+ return `${ this . _baseUrl } /${ this . _bucketPrefix + fileName } ` ;
142134 }
135+ return `https://${ this . _bucket } .s3.amazonaws.com/${ this . _bucketPrefix + fileName } ` ;
143136 }
144- return ( config . mount + ' /files/' + config . applicationId + '/' + filename ) ;
145- }
137+ return ( ` ${ config . mount } /files/${ config . applicationId } / ${ fileName } ` ) ;
138+ } ;
146139
147140S3Adapter . prototype . handleFileStream = function ( filename , req , res ) {
148141 const params = {
149142 Key : this . _bucketPrefix + filename ,
150143 Range : req . get ( 'Range' ) ,
151144 } ;
152- return this . createBucket ( ) . then ( ( ) => {
153- return new Promise ( ( resolve , reject ) => {
154- this . _s3Client . getObject ( params , ( error , data ) => {
155- if ( error !== null ) {
156- return reject ( error ) ;
157- }
158- if ( data && ! data . Body ) {
159- return reject ( data ) ;
160- }
161- res . writeHead ( 206 , {
162- 'Accept-Ranges' : data . AcceptRanges ,
163- 'Content-Length' : data . ContentLength ,
164- 'Content-Range' : data . ContentRange ,
165- 'Content-Type' : data . ContentType ,
166- } ) ;
167- res . write ( data . Body ) ;
168- res . end ( ) ;
169- resolve ( data . Body ) ;
145+ return this . createBucket ( ) . then ( ( ) => new Promise ( ( resolve , reject ) => {
146+ this . _s3Client . getObject ( params , ( error , data ) => {
147+ if ( error !== null ) {
148+ return reject ( error ) ;
149+ }
150+ if ( data && ! data . Body ) {
151+ return reject ( data ) ;
152+ }
153+ res . writeHead ( 206 , {
154+ 'Accept-Ranges' : data . AcceptRanges ,
155+ 'Content-Length' : data . ContentLength ,
156+ 'Content-Range' : data . ContentRange ,
157+ 'Content-Type' : data . ContentType ,
170158 } ) ;
159+ res . write ( data . Body ) ;
160+ res . end ( ) ;
161+ return resolve ( data . Body ) ;
171162 } ) ;
172- } ) ;
173- }
163+ } ) ) ;
164+ } ;
174165
175166module . exports = S3Adapter ;
176167module . exports . default = S3Adapter ;
0 commit comments