-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgcp.ts
48 lines (38 loc) · 1.29 KB
/
gcp.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
import { Storage, GetSignedUrlConfig } from '@google-cloud/storage';
const { BUCKET_NAME, GCP_STORAGE_SERVICE_ACCOUNT } = process.env;
if (BUCKET_NAME == null) {
throw new Error('BUCKET_NAME not set');
}
if (GCP_STORAGE_SERVICE_ACCOUNT == null) {
throw new Error('GCP_STORAGE_SERVICE_ACCOUNT not set');
}
// These options will allow temporary read access to the file
export const getSignedUploadUrl = async (filename: string): Promise<string> => {
const credentials = JSON.parse(GCP_STORAGE_SERVICE_ACCOUNT);
const storage = new Storage({ credentials });
const options: GetSignedUrlConfig = {
action: 'write' as 'write',
contentType: 'application/pdf',
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
version: 'v4' as 'v4',
};
const [url] = await storage
.bucket(BUCKET_NAME)
.file(filename)
.getSignedUrl(options);
return url;
};
export const getSignedReadUrl = async (filename: string): Promise<string> => {
const credentials = JSON.parse(GCP_STORAGE_SERVICE_ACCOUNT);
const storage = new Storage({ credentials });
const options: GetSignedUrlConfig = {
action: 'read' as 'read',
expires: Date.now() + 24 * 60 * 60 * 1000, // 7 days
version: 'v4' as 'v4',
};
const [url] = await storage
.bucket(BUCKET_NAME)
.file(filename)
.getSignedUrl(options);
return url;
};