1- import { Storage , FilesReadable } from 'storage-core'
1+ import { Storage , FilesReadable , PathAbs , StorageOptions } from 'storage-core'
22import pathModule from 'path'
3- import {
4- BlobServiceClient ,
5- StorageSharedKeyCredential ,
6- ContainerClient
7- } from '@azure/storage-blob'
8- import { AzureBlobStorageOptions } from './AzureBlobStorageOptions'
3+ import { BlobServiceClient , ContainerClient } from '@azure/storage-blob'
94import { Readable , PassThrough } from 'stream'
105import { ListResult } from 'storage-core'
6+ import {
7+ AzureStorageAccount ,
8+ AzureStorageAccountOptions
9+ } from './AzureStorageAccount'
10+
11+ export interface AzureBlobStorageOptions
12+ extends StorageOptions ,
13+ AzureStorageAccountOptions {
14+ container : string
15+ }
1116
1217export class AzureBlobStorage extends Storage < AzureBlobStorageOptions > {
1318 blobClient : BlobServiceClient
1419 containerClient : ContainerClient
1520
16- constructor ( options : AzureBlobStorageOptions ) {
21+ constructor (
22+ options : AzureBlobStorageOptions ,
23+ provider ?: AzureStorageAccount
24+ ) {
1725 super ( options )
18- const credentials = new StorageSharedKeyCredential (
19- options . accountName ,
20- options . accountKey
21- )
22- this . blobClient = new BlobServiceClient (
23- options . endpoint ||
24- `https://${ options . accountName } .blob.core.windows.net` ,
25- credentials
26- )
26+
27+ this . blobClient = ( provider ?? new AzureStorageAccount ( options ) ) . blobClient
2728 this . containerClient = this . blobClient . getContainerClient ( options . container )
2829 }
2930
31+ @PathAbs ( )
3032 async getTopLevel ( path ?: string ) : Promise < ListResult [ ] > {
3133 const iterator = this . containerClient
3234 . listBlobsByHierarchy ( '/' , { prefix : path ? `${ path } /` : '' } )
@@ -54,13 +56,15 @@ export class AzureBlobStorage extends Storage<AzureBlobStorageOptions> {
5456 return dirs . concat ( files )
5557 }
5658
59+ @PathAbs ( )
5760 async getFileSize ( path : string ) : Promise < number > {
5861 const props = await this . containerClient
5962 . getBlockBlobClient ( path )
6063 . getProperties ( )
6164 return props . contentLength || 0
6265 }
6366
67+ @PathAbs ( )
6468 getFilesStream ( path ?: string | undefined ) : Readable {
6569 let iterator = this . containerClient . listBlobsFlat ( { prefix : path } )
6670 return new FilesReadable ( async ( ) => {
@@ -80,20 +84,24 @@ export class AzureBlobStorage extends Storage<AzureBlobStorageOptions> {
8084 } )
8185 }
8286
87+ @PathAbs ( )
8388 readFile ( filePath : string ) : Promise < Buffer > {
8489 return this . containerClient . getBlockBlobClient ( filePath ) . downloadToBuffer ( )
8590 }
8691
92+ @PathAbs ( )
8793 async writeFile ( filePath : string , data : string | Buffer ) : Promise < void > {
8894 await this . containerClient
8995 . getBlockBlobClient ( filePath )
9096 . upload ( data , data . length )
9197 }
9298
99+ @PathAbs ( )
93100 async deleteFile ( filePath : string ) : Promise < void > {
94101 await this . containerClient . getBlockBlobClient ( filePath ) . delete ( )
95102 }
96103
104+ @PathAbs ( )
97105 async createWriteStream ( filePath : string ) {
98106 // Failed if file is not already created
99107 await this . writeFile ( filePath , '' )
@@ -103,6 +111,7 @@ export class AzureBlobStorage extends Storage<AzureBlobStorageOptions> {
103111 return stream
104112 }
105113
114+ @PathAbs ( )
106115 async createReadStream ( filePath : string ) {
107116 const download = await this . containerClient
108117 . getBlockBlobClient ( filePath )
0 commit comments