From 8d55b22ab6d5a94c8b337c3a0ffd35bec5629702 Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Tue, 6 Aug 2024 15:54:14 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20attempt=20to=20use=20s3client=20?= =?UTF-8?q?sdk?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/_common/grapherRenderer.ts | 30 +++++++++++++++++++-------- functions/grapher/thumbnail/[slug].ts | 1 + 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/functions/_common/grapherRenderer.ts b/functions/_common/grapherRenderer.ts index a517f0f6985..4f923031aeb 100644 --- a/functions/_common/grapherRenderer.ts +++ b/functions/_common/grapherRenderer.ts @@ -20,7 +20,7 @@ import { Env } from "../grapher/thumbnail/[slug].js" import { R2GrapherConfigDirectory } from "@ourworldindata/types" import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3" -async function getFileFromR2(key: string, env: Env) { +async function getFileFromR2(key: string, env: Env): Promise { const s3Client = new S3Client({ endpoint: env.R2_ENDPOINT, forcePathStyle: false, @@ -32,11 +32,21 @@ async function getFileFromR2(key: string, env: Env) { }) const params = { - Bucket: env.GRAPHER_CONFIG_R2_BUCKET_PATH, + Bucket: env.GRAPHER_CONFIG_R2_BUCKET, Key: key, } - const response = await s3Client.send(new GetObjectCommand(params)) + try { + console.log("preparing s3 get") + const response = await s3Client.send(new GetObjectCommand(params)) + console.log("got s3 response") + const content = await response.Body.transformToString() + console.log("got s3 content") + return content + } catch (err) { + if (err.name === "NoSuchKey") return null + else throw err + } } declare global { @@ -177,15 +187,17 @@ async function fetchAndRenderGrapherToSvg({ `${slugOnly}.json`, ]).join("/") - // Fetch grapher config - const fetchResponse = await env.r2ChartConfigs.get(key) + console.log("Fetching", key) + + // Fetch grapher config and extract it from the HTML + const grapherConfigText = await getFileFromR2(key, env) + const grapherConfig: GrapherInterface = JSON.parse(grapherConfigText) - if (!fetchResponse) { - return null + if (!grapherConfig) { + throw new Error("Could not find grapher config") } - const grapherConfig: GrapherInterface = await fetchResponse.json() - console.log("grapher interface", grapherConfig) + grapherLogger.log("fetchGrapherConfig") const bounds = new Bounds(0, 0, options.svgWidth, options.svgHeight) const grapher = new Grapher({ diff --git a/functions/grapher/thumbnail/[slug].ts b/functions/grapher/thumbnail/[slug].ts index 26fa8a35dfa..f439e8dce95 100644 --- a/functions/grapher/thumbnail/[slug].ts +++ b/functions/grapher/thumbnail/[slug].ts @@ -6,6 +6,7 @@ export interface Env { fetch: typeof fetch } url: URL + GRAPHER_CONFIG_R2_BUCKET: string GRAPHER_CONFIG_R2_BUCKET_PATH: string R2_ENDPOINT: string R2_REGION: string