Skip to content

Commit

Permalink
🚧 attempt to use s3client sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Aug 8, 2024
1 parent 230ed49 commit b052167
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
30 changes: 21 additions & 9 deletions functions/_common/grapherRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
const s3Client = new S3Client({
endpoint: env.R2_ENDPOINT,
forcePathStyle: false,
Expand All @@ -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 {
Expand Down Expand Up @@ -187,15 +197,17 @@ async function fetchAndRenderGrapherToSvg({

console.log("fetching grapher config from this key", key)

// 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 title", grapherConfig.title)
grapherLogger.log("fetchGrapherConfig")

const bounds = new Bounds(0, 0, options.svgWidth, options.svgHeight)
const grapher = new Grapher({
Expand Down
1 change: 1 addition & 0 deletions functions/grapher/thumbnail/[slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b052167

Please sign in to comment.