diff --git a/src/packages/StorageFileApi.ts b/src/packages/StorageFileApi.ts index daf3911..a267bcc 100644 --- a/src/packages/StorageFileApi.ts +++ b/src/packages/StorageFileApi.ts @@ -360,13 +360,14 @@ export default class StorageFileApi { * * @param path The full path and file name of the file to be downloaded. For example `folder/image.png`. * @param options.transform Transform the asset before serving it to the client. + * @param options.stream If set to true, the response will be a ReadableStream. Otherwise, it will be a Blob (default). */ - async download( + async download( path: string, - options?: { transform?: TransformOptions } + options?: Options ): Promise< | { - data: Blob + data: Options['stream'] extends true ? ReadableStream : Blob error: null } | { @@ -385,8 +386,16 @@ export default class StorageFileApi { headers: this.headers, noResolveJson: true, }) - const data = await res.blob() - return { data, error: null } + + if (!options?.stream) { + const data = await res.blob() + return { data, error: null } + } + + return { + data: res.body, + error: null, + } } catch (error) { if (isStorageError(error)) { return { data: null, error } diff --git a/test/storageFileApi.test.ts b/test/storageFileApi.test.ts index 1e808c2..5260a2c 100644 --- a/test/storageFileApi.test.ts +++ b/test/storageFileApi.test.ts @@ -201,6 +201,16 @@ describe('Object API', () => { expect(res.data?.type).toEqual('text/plain;charset=utf-8') }) + test('downloads an object as a stream', async () => { + await storage.from(bucketName).upload(uploadPath, file) + const res = await storage.from(bucketName).download(uploadPath, { + stream: true, + }) + + expect(res.error).toBeNull() + expect(res.data).toBeInstanceOf(ReadableStream) + }) + test('removes an object', async () => { await storage.from(bucketName).upload(uploadPath, file) const res = await storage.from(bucketName).remove([uploadPath])