-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgcs.ts
148 lines (122 loc) · 3.61 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
141
142
143
144
145
146
147
148
import { Readable, Writable } from "stream";
import { File, Bucket, Storage } from "@google-cloud/storage";
import { dir } from "~/utils";
import {
DataSourceUnreachableError,
DataSourceOverwriteError,
DataSourceError,
} from "../errors";
import { StreamReader, StreamWriter } from "../interface";
const GCS_PREFIX = new RegExp("^gs://");
export interface CredentialBody {
client_email: string;
private_key: string;
}
export type GoogleCloudStorageOptions = {
gcpProject: string;
gcpKeyFile?: string;
gcpCredentials?: CredentialBody;
gcpAdc?: boolean;
};
class GoogleCloudStorageSource {
readonly storage: Storage;
readonly bucket: Bucket;
readonly file: File;
constructor(
readonly path: string,
projectId: string,
credentials?: string | CredentialBody,
) {
if (!this.path.endsWith(".ndjson")) this.path += ".ndjson";
if (typeof credentials === "string") {
this.storage = new Storage({
projectId,
keyFile: credentials,
});
} else if (credentials) {
this.storage = new Storage({
projectId,
credentials,
});
} else {
this.storage = new Storage({ projectId });
}
const filePath = this.path.replace(GCS_PREFIX, "");
const seperator = filePath.indexOf("/");
const bucket = filePath.slice(0, seperator);
const name = filePath.slice(seperator + 1);
if (seperator === -1 || name === ".ndjson")
throw new DataSourceError(
`Invalid Google Cloud Storage file path: ${dir(this.path)}`,
);
this.bucket = this.storage.bucket(bucket);
this.file = this.bucket.file(name);
}
async assertBucketExists() {
const [bucketExists] = await this.bucket.exists();
if (!bucketExists)
throw new DataSourceUnreachableError(
"bucket does not exist or is not accessible",
);
}
async assertFileExists() {
const [fileExists] = await this.file.exists();
if (!fileExists)
throw new DataSourceUnreachableError(
"file does not exist or is not accessible",
);
}
}
export class GoogleCloudStorageReader extends StreamReader {
private source: GoogleCloudStorageSource;
protected stream?: Readable;
constructor(
path: string,
projectId: string,
credentials?: string | CredentialBody,
) {
super();
this.source = new GoogleCloudStorageSource(path, projectId, credentials);
}
override get path() {
return this.source.path;
}
async open(): Promise<void> {
await this.source.assertBucketExists();
await this.source.assertFileExists();
this.stream = this.source.file.createReadStream();
}
}
export class GoogleCloudStorageWriter extends StreamWriter {
private source: GoogleCloudStorageSource;
protected stream?: Writable;
constructor(
path: string,
projectId: string,
credentials?: string | CredentialBody,
) {
super();
this.source = new GoogleCloudStorageSource(path, projectId, credentials);
}
override get path() {
return this.source.path;
}
async open(overwrite?: boolean) {
await this.source.assertBucketExists();
// Check if file exists
let overwritten = false;
try {
await this.source.assertFileExists();
if (!overwrite) throw new DataSourceOverwriteError(this.path);
overwritten = true;
} catch (error) {
if (!(error instanceof DataSourceUnreachableError)) throw error;
}
// Create stream
return new Promise<boolean>((resolve, reject) => {
this.stream = this.source.file.createWriteStream({ resumable: false });
this.stream.on("error", (error) => reject(error));
resolve(overwritten);
});
}
}