@@ -4,6 +4,7 @@ var downloader = require('s3-download-stream');
4
4
var debug = require ( 'debug' ) ( 's3-blob-store' ) ;
5
5
var mime = require ( 'mime-types' ) ;
6
6
var uploadStream = require ( 's3-stream-upload' ) ;
7
+ var util = require ( 'util' ) ;
7
8
8
9
/**
9
10
* Create S3 blob store
@@ -17,21 +18,20 @@ function S3BlobStore (opts) {
17
18
opts = opts || { } ;
18
19
if ( ! opts . client ) throw Error ( 'S3BlobStore client option required (aws-sdk AWS.S3 instance)' ) ;
19
20
if ( ! opts . bucket ) throw Error ( 'S3BlobStore bucket option required' ) ;
20
- this . accessKey = opts . accessKey ;
21
- this . secretKey = opts . secretKey ;
22
21
this . bucket = opts . bucket ;
23
22
this . s3 = opts . client ;
24
23
}
25
24
26
25
/**
27
26
* Create read stream
28
27
* @param {ReadStreamOptions|String } opts options or object key
28
+ * @param {ReadParams } [s3opts] additional S3 options
29
29
* @returns {ReadableStream }
30
30
* readable stream of data for the file in your bucket whose key matches
31
31
*/
32
- S3BlobStore . prototype . createReadStream = function ( opts ) {
32
+ S3BlobStore . prototype . createReadStream = function ( opts , s3opts ) {
33
33
if ( typeof opts === 'string' ) opts = { key : opts } ;
34
- var config = { client : this . s3 , params : this . downloadParams ( opts ) } ;
34
+ var config = { client : this . s3 , params : this . _s3params ( opts , s3opts ) } ;
35
35
if ( opts . concurrency ) config . concurrency = opts . concurrency ;
36
36
if ( opts . chunkSize ) config . chunkSize = opts . chunkSize ;
37
37
var stream = downloader ( config ) ;
@@ -41,36 +41,10 @@ S3BlobStore.prototype.createReadStream = function (opts) {
41
41
return stream ;
42
42
} ;
43
43
44
- S3BlobStore . prototype . uploadParams = function ( opts ) {
45
- opts = Object . assign ( { } , opts , {
46
- params : Object . assign ( { } , opts . params )
47
- } ) ;
48
-
49
- var filename = opts . name || opts . filename ;
50
- var key = opts . key || filename ;
51
- var contentType = opts . contentType ;
52
-
53
- var params = opts . params ;
54
- params . Bucket = params . Bucket || this . bucket ;
55
- params . Key = params . Key || key ;
56
-
57
- if ( ! contentType ) {
58
- contentType = filename ? mime . lookup ( filename ) : mime . lookup ( opts . key ) ;
59
- }
60
- if ( contentType ) params . ContentType = contentType ;
61
-
62
- return params ;
63
- } ;
64
-
65
- S3BlobStore . prototype . downloadParams = function ( opts ) {
66
- var params = this . uploadParams ( opts ) ;
67
- delete params . ContentType ;
68
- return params ;
69
- } ;
70
-
71
44
/**
72
45
* Create write stream
73
46
* @param {Options<WriteParams>|String } opts options or object key
47
+ * @param {WriteParams } [s3opts] additional S3 options
74
48
* @param {function(Error, { key: String }) } done callback
75
49
* @returns {WritableStream } writable stream that you can pipe data to
76
50
*/
@@ -80,7 +54,9 @@ S3BlobStore.prototype.createWriteStream = function (opts, s3opts, done) {
80
54
s3opts = { } ;
81
55
}
82
56
if ( typeof opts === 'string' ) opts = { key : opts } ;
83
- var params = this . uploadParams ( opts ) ;
57
+ var params = this . _s3params ( opts , s3opts ) ;
58
+ var contentType = ( opts && opts . contentType ) || mime . lookup ( params . Key ) ;
59
+ if ( contentType ) params . ContentType = contentType ;
84
60
var out = uploadStream ( this . s3 , params ) ;
85
61
out . on ( 'error' , function ( err ) {
86
62
debug ( 'got err %j' , err ) ;
@@ -95,28 +71,63 @@ S3BlobStore.prototype.createWriteStream = function (opts, s3opts, done) {
95
71
96
72
/**
97
73
* Remove object from store
98
- * @param {{ key: String }|String } opts options containing object key or just key
74
+ * @param {Options<RemoveParams>|String } opts options or object key
75
+ * @param {RemoveParams } [s3opts] additional S3 options
99
76
* @param {function(Error) } done callback
100
77
*/
101
- S3BlobStore . prototype . remove = function ( opts , done ) {
102
- var key = typeof opts === 'string' ? opts : opts . key ;
103
- this . s3 . deleteObject ( { Bucket : this . bucket , Key : key } , done ) ;
78
+ S3BlobStore . prototype . remove = function ( opts , s3opts , done ) {
79
+ if ( typeof s3opts === 'function' ) {
80
+ done = s3opts ;
81
+ s3opts = { } ;
82
+ }
83
+ if ( typeof opts === 'string' ) opts = { key : opts } ;
84
+ var params = this . _s3params ( opts , s3opts ) ;
85
+ this . s3 . deleteObject ( params , done ) ;
104
86
return this ;
105
87
} ;
106
88
107
89
/**
108
90
* Check if object exits
109
- * @param {{ key: String }|String } opts options containing object key or just key
91
+ * @param {Options<ExistsParams>|String } opts options or object key
92
+ * @param {ExistsParams } [s3opts] additional S3 options
110
93
* @param {function(Error, Boolean) } done callback
111
94
*/
112
- S3BlobStore . prototype . exists = function ( opts , done ) {
95
+ S3BlobStore . prototype . exists = function ( opts , s3opts , done ) {
96
+ if ( typeof s3opts === 'function' ) {
97
+ done = s3opts ;
98
+ s3opts = { } ;
99
+ }
113
100
if ( typeof opts === 'string' ) opts = { key : opts } ;
114
- this . s3 . headObject ( { Bucket : this . bucket , Key : opts . key } , function ( err , res ) {
101
+ var params = this . _s3params ( opts , s3opts ) ;
102
+ this . s3 . headObject ( params , function ( err , _res ) {
115
103
if ( err && err . statusCode === 404 ) return done ( null , false ) ;
116
104
done ( err , ! err ) ;
117
105
} ) ;
118
106
} ;
119
107
108
+ S3BlobStore . prototype . _s3params = function ( opts , s3opts ) {
109
+ opts = opts || { } ;
110
+ opts . params = s3opts || opts . params || { } ;
111
+ var key = opts . key || opts . name || opts . filename ;
112
+ var params = Object . assign ( { } , opts . params , {
113
+ Bucket : opts . params . Bucket || this . bucket ,
114
+ Key : opts . params . Key || key
115
+ } ) ;
116
+ return params ;
117
+ } ;
118
+
119
+ S3BlobStore . prototype . uploadParams = util . deprecate ( function ( opts ) {
120
+ opts = opts || { } ;
121
+ var params = this . _s3params ( opts ) ;
122
+ var contentType = opts . contentType || mime . lookup ( params . Key ) ;
123
+ if ( contentType ) params . ContentType = contentType ;
124
+ return params ;
125
+ } , 'S3BlobStore#uploadParams(opts) is deprecated and will be removed in upcoming v5!' ) ;
126
+
127
+ S3BlobStore . prototype . downloadParams = util . deprecate ( function ( opts ) {
128
+ return this . _s3params ( opts ) ;
129
+ } , 'S3BlobStore#downloadParams(opts) is deprecated and will be removed in upcoming v5!' ) ;
130
+
120
131
module . exports = S3BlobStore ;
121
132
122
133
/** @typedef {import('stream').Readable } ReadableStream */
@@ -158,3 +169,17 @@ module.exports = S3BlobStore;
158
169
* @name WriteParams
159
170
* @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
160
171
*/
172
+
173
+ /**
174
+ * S3 `deleteObject` params
175
+ * @typedef {import('aws-sdk').S3.DeleteObjectRequest } RemoveParams
176
+ * @name RemoveParams
177
+ * @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#deleteObject-property
178
+ */
179
+
180
+ /**
181
+ * S3 `headObject` params
182
+ * @typedef {import('aws-sdk').S3.HeadObjectRequest } ExistsParams
183
+ * @name ExistsParams
184
+ * @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#headObject-property
185
+ */
0 commit comments