-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
chartConfigR2Helpers.ts
170 lines (154 loc) · 4.56 KB
/
chartConfigR2Helpers.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
GRAPHER_CONFIG_R2_BUCKET,
GRAPHER_CONFIG_R2_BUCKET_PATH,
R2_ACCESS_KEY_ID,
R2_ENDPOINT,
R2_REGION,
R2_SECRET_ACCESS_KEY,
} from "../settings/serverSettings.js"
import {
DeleteObjectCommand,
DeleteObjectCommandInput,
PutObjectCommand,
PutObjectCommandInput,
S3Client,
} from "@aws-sdk/client-s3"
import { JsonError, lazy } from "@ourworldindata/utils"
import { Base64String, R2GrapherConfigDirectory } from "@ourworldindata/types"
import { logErrorAndMaybeSendToBugsnag } from "../serverUtils/errorLog.js"
const getS3Client: () => S3Client = lazy(
() =>
new S3Client({
endpoint: R2_ENDPOINT,
forcePathStyle: false,
region: R2_REGION,
credentials: {
accessKeyId: R2_ACCESS_KEY_ID,
secretAccessKey: R2_SECRET_ACCESS_KEY,
},
})
)
export async function saveGrapherConfigToR2ByUUID(
uuid: string,
chartConfigStringified: string,
configMd5FromDb: Base64String
) {
await saveGrapherConfigToR2(
chartConfigStringified,
R2GrapherConfigDirectory.byUUID,
`${uuid}.json`,
configMd5FromDb
)
}
export async function deleteGrapherConfigFromR2ByUUID(id: string) {
await deleteGrapherConfigFromR2(
R2GrapherConfigDirectory.byUUID,
`${id}.json`
)
}
export async function saveGrapherConfigToR2(
config: string,
directory: R2GrapherConfigDirectory,
filename: string,
configMd5FromDb: Base64String
) {
await saveConfigToR2(config, directory, filename, configMd5FromDb)
}
export async function saveMultiDimConfigToR2(
config: string,
slug: string,
configMd5FromDb: Base64String
) {
await saveConfigToR2(
config,
R2GrapherConfigDirectory.multiDim,
`${slug}.json`,
configMd5FromDb
)
}
async function saveConfigToR2(
config: string,
directory: string,
filename: string,
configMd5FromDb: Base64String
) {
if (process.env.NODE_ENV === "test") {
console.log("Skipping saving config to R2 in test environment")
return
}
if (
GRAPHER_CONFIG_R2_BUCKET === undefined ||
GRAPHER_CONFIG_R2_BUCKET_PATH === undefined
) {
console.info("R2 bucket not configured, not storing config to R2")
return
}
try {
const s3Client = getS3Client()
if (!GRAPHER_CONFIG_R2_BUCKET || !GRAPHER_CONFIG_R2_BUCKET_PATH) {
throw new Error("R2 bucket not configured")
}
const bucket = GRAPHER_CONFIG_R2_BUCKET
const path = [GRAPHER_CONFIG_R2_BUCKET_PATH, directory, filename].join(
"/"
)
const MIMEType = "application/json"
const params: PutObjectCommandInput = {
Bucket: bucket,
Key: path,
Body: config,
ContentType: MIMEType,
ContentMD5: configMd5FromDb,
}
await s3Client.send(new PutObjectCommand(params))
console.log(
`Successfully uploaded object: ${params.Bucket}/${params.Key}`
)
} catch (err) {
await logErrorAndMaybeSendToBugsnag(err)
throw new JsonError(
`Failed to save the config to R2. Inner error: ${err}`
)
}
}
export async function deleteGrapherConfigFromR2(
directory: R2GrapherConfigDirectory,
filename: string
) {
if (process.env.NODE_ENV === "test") {
console.log("Skipping saving grapher config to R2 in test environment")
return
}
if (
GRAPHER_CONFIG_R2_BUCKET === undefined ||
GRAPHER_CONFIG_R2_BUCKET_PATH === undefined
) {
console.info(
"R2 bucket not configured, not deleting grapher config to R2"
)
return
}
try {
const s3Client = getS3Client()
if (!GRAPHER_CONFIG_R2_BUCKET || !GRAPHER_CONFIG_R2_BUCKET_PATH) {
throw new Error("R2 bucket not configured")
}
const bucket = GRAPHER_CONFIG_R2_BUCKET
const path = [GRAPHER_CONFIG_R2_BUCKET_PATH, directory, filename].join(
"/"
)
const params: DeleteObjectCommandInput = {
Bucket: bucket,
Key: path,
}
await s3Client.send(new DeleteObjectCommand(params))
console.log(
`Successfully deleted object: ${params.Bucket}/${params.Key}`
)
} catch (err) {
await logErrorAndMaybeSendToBugsnag(err)
throw new JsonError(
`Failed to delete the grapher config to R2 at ${directory}/${filename}. Inner error: ${err}`
)
}
}