Skip to content

Commit 8a83dd6

Browse files
committed
Added toplevel support for azure blob storage
1 parent 31447c8 commit 8a83dd6

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

packages/azure/src/AzureBlobStorage.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Storage, FilesReadable } from 'storage-core'
2+
import pathModule from 'path'
23
import {
34
BlobServiceClient,
45
StorageSharedKeyCredential,
@@ -26,8 +27,32 @@ export class AzureBlobStorage extends Storage<AzureBlobStorageOptions> {
2627
this.containerClient = this.blobClient.getContainerClient(options.container)
2728
}
2829

29-
getTopLevel(_?: string): Promise<ListResult[]> {
30-
throw new Error('Method not implemented.')
30+
async getTopLevel(path?: string): Promise<ListResult[]> {
31+
const iterator = this.containerClient
32+
.listBlobsByHierarchy('/', { prefix: path ? `${path}/` : '' })
33+
.byPage({ maxPageSize: 1000 })
34+
35+
const { value } = await iterator.next()
36+
const { blobItems, blobPrefixes } = value.segment
37+
38+
const dirs: ListResult[] = blobPrefixes.map((b: any) => ({
39+
name: pathModule.basename(b.name),
40+
path: b.name.slice(0, -1),
41+
isFile: false,
42+
raw: b
43+
}))
44+
45+
const files: ListResult[] = blobItems.map((b: any) => ({
46+
name: pathModule.basename(b.name),
47+
path: b.name,
48+
size: b.properties.contentLength,
49+
createdAt: b.properties.createdOn,
50+
lastModified: b.properties.lastModified,
51+
isFile: true,
52+
raw: b
53+
}))
54+
55+
return dirs.concat(files)
3156
}
3257

3358
async getFileSize(path: string): Promise<number> {
@@ -39,7 +64,6 @@ export class AzureBlobStorage extends Storage<AzureBlobStorageOptions> {
3964

4065
getFilesStream(path?: string | undefined): Readable {
4166
let iterator = this.containerClient.listBlobsFlat({ prefix: path })
42-
4367
return new FilesReadable(async () => {
4468
const { value } = await iterator.next()
4569
return (

packages/core/src/LocalStorage.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ export class LocalStorage extends Storage<LocalStorageOptions> {
3838
onlyDirectories: false,
3939
objectMode: true
4040
})
41+
42+
const rootPath = pathModule.resolve(this.fullPath())
43+
const formatPath = (path: string) =>
44+
pathModule.resolve(path).replace(`${rootPath}/`, '')
45+
4146
return files.map(f => ({
42-
path: f.path,
47+
path: formatPath(f.path),
4348
name: pathModule.basename(f.path),
4449
raw: f,
4550
isFile: f.dirent.isFile()
@@ -48,9 +53,14 @@ export class LocalStorage extends Storage<LocalStorageOptions> {
4853

4954
getFilesStream(path?: string): Readable {
5055
const fullPath = this.fullPath(path)
56+
57+
const rootPath = pathModule.resolve(this.fullPath())
58+
const formatPath = (path: string) =>
59+
pathModule.resolve(path).replace(`${rootPath}/`, '')
60+
5161
const trans = new FilesTransform((f: any) => ({
5262
name: pathModule.basename(f.path),
53-
path: f.path,
63+
path: formatPath(f.path),
5464
size: f.stats.size,
5565
raw: f
5666
}))

packages/samples/ts/AzureBlobStorage.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ import { getStorageManager, getStorageFactory } from 'storage-core'
5050
for await (let path of ls.getFilesStream('azure-write')) {
5151
console.log(path.name)
5252
}
53+
54+
for (let result of await ls.getTopLevel('azure-write')) {
55+
console.log(`${result.isFile} | ${result.path}`)
56+
}
5357
})()

packages/samples/ts/LocalStorage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ import { LocalStorage, getStorageManager } from 'storage-core'
4343
}
4444

4545
for (let result of await ls.getTopLevel('local-write')) {
46-
console.log(`${result.isFile} | ${result.name}`)
46+
console.log(`${result.isFile} | ${result.path}`)
4747
}
4848
})()

0 commit comments

Comments
 (0)