@@ -4,6 +4,7 @@ var downloader = require('s3-download-stream');
44var debug = require ( 'debug' ) ( 's3-blob-store' ) ;
55var mime = require ( 'mime-types' ) ;
66var uploadStream = require ( 's3-stream-upload' ) ;
7+ var util = require ( 'util' ) ;
78
89/**
910 * Create S3 blob store
@@ -17,21 +18,20 @@ function S3BlobStore (opts) {
1718 opts = opts || { } ;
1819 if ( ! opts . client ) throw Error ( 'S3BlobStore client option required (aws-sdk AWS.S3 instance)' ) ;
1920 if ( ! opts . bucket ) throw Error ( 'S3BlobStore bucket option required' ) ;
20- this . accessKey = opts . accessKey ;
21- this . secretKey = opts . secretKey ;
2221 this . bucket = opts . bucket ;
2322 this . s3 = opts . client ;
2423}
2524
2625/**
2726 * Create read stream
2827 * @param {ReadStreamOptions|String } opts options or object key
28+ * @param {ReadParams } [s3opts] additional S3 options
2929 * @returns {ReadableStream }
3030 * readable stream of data for the file in your bucket whose key matches
3131 */
32- S3BlobStore . prototype . createReadStream = function ( opts ) {
32+ S3BlobStore . prototype . createReadStream = function ( opts , s3opts ) {
3333 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 ) } ;
3535 if ( opts . concurrency ) config . concurrency = opts . concurrency ;
3636 if ( opts . chunkSize ) config . chunkSize = opts . chunkSize ;
3737 var stream = downloader ( config ) ;
@@ -41,36 +41,10 @@ S3BlobStore.prototype.createReadStream = function (opts) {
4141 return stream ;
4242} ;
4343
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-
7144/**
7245 * Create write stream
7346 * @param {Options<WriteParams>|String } opts options or object key
47+ * @param {WriteParams } [s3opts] additional S3 options
7448 * @param {function(Error, { key: String }) } done callback
7549 * @returns {WritableStream } writable stream that you can pipe data to
7650 */
@@ -80,7 +54,9 @@ S3BlobStore.prototype.createWriteStream = function (opts, s3opts, done) {
8054 s3opts = { } ;
8155 }
8256 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 ;
8460 var out = uploadStream ( this . s3 , params ) ;
8561 out . on ( 'error' , function ( err ) {
8662 debug ( 'got err %j' , err ) ;
@@ -95,28 +71,63 @@ S3BlobStore.prototype.createWriteStream = function (opts, s3opts, done) {
9571
9672/**
9773 * 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
9976 * @param {function(Error) } done callback
10077 */
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 ) ;
10486 return this ;
10587} ;
10688
10789/**
10890 * 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
11093 * @param {function(Error, Boolean) } done callback
11194 */
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+ }
113100 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 ) {
115103 if ( err && err . statusCode === 404 ) return done ( null , false ) ;
116104 done ( err , ! err ) ;
117105 } ) ;
118106} ;
119107
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+
120131module . exports = S3BlobStore ;
121132
122133/** @typedef {import('stream').Readable } ReadableStream */
@@ -158,3 +169,17 @@ module.exports = S3BlobStore;
158169 * @name WriteParams
159170 * @see https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
160171 */
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