-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgcs.ts
140 lines (114 loc) · 4.32 KB
/
gcs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2019 - present Nimbella Corp.
*
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {
StorageProvider, StorageClient, RemoteFile, DeleteFilesOptions, DownloadOptions, GetFilesOptions,
SaveOptions, SignedUrlOptions, UploadOptions, StorageKey, FileMetadata, WebsiteOptions, SettableFileMetadata
} from './interface'
import { Storage, Bucket, File, GetSignedUrlConfig } from '@google-cloud/storage'
import { isWeb, getBucketFromCredentials } from './common'
class GCSRemoteFile implements RemoteFile {
private file: File
name: string
constructor(file: File) {
this.file = file
this.name = file.name
}
getImplementation(): any {
return this.file
}
save(data: Buffer, options: SaveOptions): Promise<any> {
return this.file.save(data, options)
}
setMetadata(meta: SettableFileMetadata): Promise<any> {
return this.file.setMetadata(meta)
}
async getMetadata(): Promise<FileMetadata> {
const data = await this.file.getMetadata()
return data[0]
}
async exists(): Promise<boolean> {
const ans = await this.file.exists()
return ans[0]
}
delete(): Promise<any> {
return this.file.delete()
}
async download(options?: DownloadOptions): Promise<Buffer> {
const response = await this.file.download(options)
return response[0]
}
async getSignedUrl(options: SignedUrlOptions): Promise<string> {
const result = await this.file.getSignedUrl(options as GetSignedUrlConfig)
return result[0]
}
}
class GCSClient implements StorageClient {
private bucket: Bucket
private url: string
constructor(bucket: Bucket, url = '') {
this.bucket = bucket
this.url = url
}
getImplementation(): any {
return this.bucket
}
getURL(): string {
return this.url
}
setWebsite(website: WebsiteOptions): Promise<any> {
return this.bucket.setMetadata({ website })
}
deleteFiles(options?: DeleteFilesOptions): Promise<any> {
return this.bucket.deleteFiles(options)
}
file(destination: string): RemoteFile {
return new GCSRemoteFile(this.bucket.file(destination))
}
upload(path: string, options?: UploadOptions): Promise<any> {
return this.bucket.upload(path, options)
}
async getFiles(options?: GetFilesOptions): Promise<RemoteFile[]> {
const files = (await this.bucket.getFiles(options))[0]
return files.map(file => new GCSRemoteFile(file))
}
}
// Compute the actual name of a bucket as viewed by google storage
function computeBucketStorageName(apiHost: string, namespace: string, web: boolean): string {
const bdn = computeBucketDomainName(apiHost, namespace).split('.').join('-')
return web ? bdn : `data-${bdn}`
}
// Compute the external domain name corresponding to a web bucket
function computeBucketDomainName(apiHost: string, namespace: string): string {
const url = new URL(apiHost)
return namespace + '-' + url.hostname
}
const provider: StorageProvider = {
prepareCredentials: (original: Record<string, any>): StorageKey => {
const { client_email, private_key, project_id } = original
return { credentials: { client_email, private_key }, project_id, provider: '@nimbella/storage-gcs' }
},
getClient: (namespace: string, apiHost: string, type: boolean|string, credentials: Record<string, any>) => {
Object.assign(credentials, { projectId: credentials.project_id })
const storage: Storage = new Storage(credentials)
const web = isWeb(type)
const bucketName = getBucketFromCredentials(type, credentials) || computeBucketStorageName(apiHost, namespace, web)
const bucket = storage.bucket(bucketName)
if (web) {
const url = `https://${computeBucketDomainName(apiHost, namespace)}`
return new GCSClient(bucket, url)
}
return new GCSClient(bucket)
},
identifier: '@nimbella/storage-gcs'
}
export default provider