Skip to content

Commit 1a6d9b1

Browse files
committed
Add storage provider class
1 parent 7e33ace commit 1a6d9b1

19 files changed

+388
-404
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
"release": "yarn clean && yarn build && lerna publish"
1111
},
1212
"devDependencies": {
13-
"@types/node": "^13.13.4",
14-
"lerna": "^3.20.2",
15-
"prettier": "^2.0.5",
13+
"@types/node": "^14.6.2",
14+
"lerna": "^3.22.1",
15+
"prettier": "^2.1.1",
1616
"rimraf": "^3.0.2",
17-
"ts-node": "^8.9.1",
18-
"typescript": "^3.8.3"
17+
"ts-node": "^9.0.0",
18+
"typescript": "^4.0.2"
1919
},
2020
"files": [
2121
"!tsconfig*"
@@ -24,6 +24,6 @@
2424
"packages/*"
2525
],
2626
"dependencies": {
27-
"storage-gcp": "^0.1.13"
27+
"storage-gcp": "^0.6.4"
2828
}
2929
}

packages/aws/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
"gitHead": "b05db75f50e3a44fd01b8c95d114f3fb5c9801c3",
2525
"dependencies": {
26-
"aws-sdk": "^2.678.0",
26+
"aws-sdk": "^2.743.0",
2727
"storage-core": "^0.6.2"
2828
},
2929
"keywords": [

packages/aws/src/AwsS3Storage.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Storage, FilesReadable, ListResult } from 'storage-core'
1+
import { Storage, FilesReadable, ListResult, PathAbs } from 'storage-core'
22
import { S3, AWSError } from 'aws-sdk'
33
import pathModule from 'path'
44

@@ -24,6 +24,7 @@ export class AwsS3Storage extends Storage<AwsS3StorageOptions> {
2424
this.s3 = new S3({ ...options })
2525
}
2626

27+
@PathAbs()
2728
async getTopLevel(path?: string): Promise<ListResult[]> {
2829
const request = await this.s3
2930
.listObjectsV2({
@@ -55,6 +56,7 @@ export class AwsS3Storage extends Storage<AwsS3StorageOptions> {
5556
return dirs.concat(files)
5657
}
5758

59+
@PathAbs()
5860
getFilesStream(path?: string): Readable {
5961
let request: PromiseResult<S3.ListObjectsV2Output, AWSError> | undefined
6062
let ContinuationToken: string | undefined
@@ -81,6 +83,7 @@ export class AwsS3Storage extends Storage<AwsS3StorageOptions> {
8183
})
8284
}
8385

86+
@PathAbs()
8487
async readFile(filePath: string): Promise<Buffer> {
8588
const r = await this.s3
8689
.getObject({ Bucket: this.options.bucket, Key: filePath })
@@ -103,6 +106,7 @@ export class AwsS3Storage extends Storage<AwsS3StorageOptions> {
103106
return buffer
104107
}
105108

109+
@PathAbs()
106110
async writeFile(filePath: string, data: string | Buffer): Promise<void> {
107111
await this.s3
108112
.putObject({
@@ -113,19 +117,22 @@ export class AwsS3Storage extends Storage<AwsS3StorageOptions> {
113117
.promise()
114118
}
115119

120+
@PathAbs()
116121
async deleteFile(filePath: string): Promise<void> {
117122
await this.s3
118123
.deleteObject({ Bucket: this.options.bucket, Key: filePath })
119124
.promise()
120125
}
121126

127+
@PathAbs()
122128
async getFileSize(filePath: string): Promise<number> {
123129
const r = await this.s3
124130
.headObject({ Bucket: this.options.bucket, Key: filePath })
125131
.promise()
126132
return r.ContentLength || 0
127133
}
128134

135+
@PathAbs()
129136
async createWriteStream(filePath: string): Promise<Writable> {
130137
const pass = new PassThrough()
131138
this.s3
@@ -134,6 +141,7 @@ export class AwsS3Storage extends Storage<AwsS3StorageOptions> {
134141
return pass
135142
}
136143

144+
@PathAbs()
137145
async createReadStream(filePath: string): Promise<Readable> {
138146
return this.s3
139147
.getObject({ Bucket: this.options.bucket, Key: filePath })

packages/azure/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
"gitHead": "b05db75f50e3a44fd01b8c95d114f3fb5c9801c3",
2525
"dependencies": {
26-
"@azure/storage-blob": "^12.1.1",
26+
"@azure/storage-blob": "^12.2.0-preview.1",
2727
"storage-core": "^0.6.2"
2828
},
2929
"keywords": [

packages/azure/src/AzureBlobStorage.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
1-
import { Storage, FilesReadable } from 'storage-core'
1+
import { Storage, FilesReadable, PathAbs, StorageOptions } from 'storage-core'
22
import 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'
94
import { Readable, PassThrough } from 'stream'
105
import { 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

1217
export 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)

packages/azure/src/AzureBlobStorageOptions.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {
2+
BlobServiceClient,
3+
StorageSharedKeyCredential
4+
} from '@azure/storage-blob'
5+
import { StorageProvider } from 'storage-core'
6+
import { AzureBlobStorage } from './AzureBlobStorage'
7+
8+
export interface AzureStorageAccountOptions {
9+
accountName: string
10+
accountKey: string
11+
endpoint?: string
12+
}
13+
14+
export class AzureStorageAccount extends StorageProvider<AzureBlobStorage> {
15+
blobClient: BlobServiceClient
16+
17+
constructor(private options: AzureStorageAccountOptions) {
18+
super()
19+
20+
const credentials = new StorageSharedKeyCredential(
21+
options.accountName,
22+
options.accountKey
23+
)
24+
this.blobClient = new BlobServiceClient(
25+
options.endpoint ||
26+
`https://${options.accountName}.blob.core.windows.net`,
27+
credentials
28+
)
29+
}
30+
31+
getStorage(name: string) {
32+
return new Promise<AzureBlobStorage>(resolve =>
33+
resolve(new AzureBlobStorage({ ...this.options, container: name }))
34+
)
35+
}
36+
37+
async createStorage(name: string) {
38+
const container = await this.blobClient.createContainer(name)
39+
const storage = new AzureBlobStorage({ ...this.options, container: name })
40+
storage.containerClient = container.containerClient
41+
return storage
42+
}
43+
44+
deleteStorage(name: string) {
45+
return this.blobClient.deleteContainer(name)
46+
}
47+
}

packages/azure/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { AzureBlobStorageOptions } from './AzureBlobStorageOptions'
2-
export { AzureBlobStorage } from './AzureBlobStorage'
1+
export * from './AzureBlobStorage'
2+
export * from './AzureStorageAccount'

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
},
2424
"dependencies": {
2525
"@types/lru-cache": "^5.1.0",
26-
"globby": "^11.0.0",
27-
"lru-cache": "^5.1.1"
26+
"globby": "^11.0.1",
27+
"lru-cache": "^6.0.0"
2828
},
2929
"gitHead": "b05db75f50e3a44fd01b8c95d114f3fb5c9801c3",
3030
"keywords": [

packages/core/src/Factory.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export type FactoryStorageOption<O extends StorageOptions = any> = O & {
55
type: string
66
}
77

8+
export type FactoryProviderOptions<O = any> = O & { type: string }
9+
810
export class StorageFactory {
911
private map = new Map<string, new (options: any) => Storage<any>>()
1012

0 commit comments

Comments
 (0)