Skip to content

Commit

Permalink
🚧 work on fetching from R2
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Aug 7, 2024
1 parent 97ecd6d commit 7ae1ef9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
21 changes: 21 additions & 0 deletions functions/_common/grapherRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ import LatoMedium from "../_common/fonts/LatoLatin-Medium.ttf.bin"
import LatoBold from "../_common/fonts/LatoLatin-Bold.ttf.bin"
import PlayfairSemiBold from "../_common/fonts/PlayfairDisplayLatin-SemiBold.ttf.bin"
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) {
const s3Client = new S3Client({
endpoint: env.R2_ENDPOINT,
forcePathStyle: false,
region: env.R2_REGION,
credentials: {
accessKeyId: env.R2_ACCESS_KEY_ID,
secretAccessKey: env.R2_SECRET_ACCESS_KEY,
},
})

const params = {
Bucket: env.GRAPHER_CONFIG_R2_BUCKET_PATH,
Key: key,
}

const response = await s3Client.send(new GetObjectCommand(params))
}

declare global {
// eslint-disable-next-line no-var
Expand Down
4 changes: 4 additions & 0 deletions functions/grapher/thumbnail/[slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface Env {
}
url: URL
GRAPHER_CONFIG_R2_BUCKET_PATH: string
R2_ENDPOINT: string
R2_REGION: string
R2_ACCESS_KEY_ID: string
R2_SECRET_ACCESS_KEY: string
CF_PAGES_BRANCH: string
}

Expand Down
1 change: 1 addition & 0 deletions functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "owid-functions",
"dependencies": {
"@aws-sdk/client-s3": "3.572.0",
"@ourworldindata/grapher": "workspace:^",
"@ourworldindata/utils": "workspace:^",
"itty-router": "^5.0.17",
Expand Down
2 changes: 2 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pages_build_output_dir = "./localBake"
[vars]
ENV = "development"
GRAPHER_CONFIG_R2_BUCKET = "owid-grapher-configs-staging"
R2_ENDPOINT = "https://078fcdfed9955087315dd86792e71a7e.r2.cloudflarestorage.com"
R2_REGION = "auto"

# Overrides for CF preview deployments
[env.preview.vars]
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ __metadata:
languageName: node
linkType: hard

"@aws-sdk/client-s3@npm:^3.572.0":
"@aws-sdk/client-s3@npm:3.572.0, @aws-sdk/client-s3@npm:^3.572.0":
version: 3.572.0
resolution: "@aws-sdk/client-s3@npm:3.572.0"
dependencies:
Expand Down Expand Up @@ -15274,6 +15274,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "owid-functions@workspace:functions"
dependencies:
"@aws-sdk/client-s3": "npm:3.572.0"
"@cloudflare/workers-types": "npm:^4.20240614.0"
"@ourworldindata/grapher": "workspace:^"
"@ourworldindata/utils": "workspace:^"
Expand Down

0 comments on commit 7ae1ef9

Please sign in to comment.