File tree Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Expand file tree Collapse file tree 2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change @@ -521,13 +521,14 @@ export default class StorageFileApi {
521
521
*
522
522
* @param path The full path and file name of the file to be downloaded. For example `folder/image.png`.
523
523
* @param options.transform Transform the asset before serving it to the client.
524
+ * @param options.stream If set to true, the response will be a ReadableStream. Otherwise, it will be a Blob (default).
524
525
*/
525
- async download (
526
+ async download < Options extends { transform ?: TransformOptions , stream ?: boolean } > (
526
527
path : string ,
527
- options ?: { transform ?: TransformOptions }
528
+ options ?: Options
528
529
) : Promise <
529
530
| {
530
- data : Blob
531
+ data : Options [ 'stream' ] extends true ? ReadableStream : Blob
531
532
error : null
532
533
}
533
534
| {
@@ -546,8 +547,16 @@ export default class StorageFileApi {
546
547
headers : this . headers ,
547
548
noResolveJson : true ,
548
549
} )
549
- const data = await res . blob ( )
550
- return { data, error : null }
550
+
551
+ if ( ! options ?. stream ) {
552
+ const data = await res . blob ( )
553
+ return { data, error : null }
554
+ }
555
+
556
+ return {
557
+ data : res . body ,
558
+ error : null ,
559
+ }
551
560
} catch ( error ) {
552
561
if ( this . shouldThrowOnError ) {
553
562
throw error
Original file line number Diff line number Diff line change @@ -420,6 +420,16 @@ describe('Object API', () => {
420
420
) . rejects . toThrow ( )
421
421
} )
422
422
423
+ test ( 'downloads an object as a stream' , async ( ) => {
424
+ await storage . from ( bucketName ) . upload ( uploadPath , file )
425
+ const res = await storage . from ( bucketName ) . download ( uploadPath , {
426
+ stream : true ,
427
+ } )
428
+
429
+ expect ( res . error ) . toBeNull ( )
430
+ expect ( res . data ) . toBeInstanceOf ( ReadableStream )
431
+ } )
432
+
423
433
test ( 'removes an object' , async ( ) => {
424
434
await storage . from ( bucketName ) . upload ( uploadPath , file )
425
435
const res = await storage . from ( bucketName ) . remove ( [ uploadPath ] )
You can’t perform that action at this time.
0 commit comments