From 36a7e1dfa45c11054b03c1aea037f3a5a7cbe61b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20M=C3=A9tral?= Date: Fri, 22 May 2020 10:52:05 +0200 Subject: [PATCH] fix: use plugin options for AWS config (#45) * chore: change env var names to ensure config comes from plugin options If the AWS credentials are not the env vars that AWS expects, requests fail (bad request). In some cases, the environment variables can not be the ones that AWS expects, because they are reserved (by Netlify, for example). This means that the config doesn't come from the plugin, as it should, and falls back on the environment variables. Changing the names in the example ensures that this will not happen in the future. * fix: revert to getting pre-signed URL in sourceNodes The AWS config wasn't properly passed in onCreateNode, and the plugin therefore relied on the AWS official environment variables being set (as a fallback). This commit fixes it, but it also reintroduces the unwanted url key on s3Object. This will be addressed in a follow-up PR. * fix: pass new env var names to GitHub Actions workflow --- .github/workflows/test.yml | 6 ++--- .../gatsby-starter-source-s3/gatsby-config.js | 6 ++--- src/gatsby-node.ts | 23 ++++++++++--------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cbcd9b08..f53688a3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,7 @@ jobs: run: yarn lint - name: E2E env: - AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_REGION: ${{ secrets.AWS_REGION }} + AWS_ACCESS_KEY_ID_: ${{ secrets.AWS_ACCESS_KEY_ID_ }} + AWS_SECRET_ACCESS_KEY_: ${{ secrets.AWS_SECRET_ACCESS_KEY_ }} + AWS_REGION_: ${{ secrets.AWS_REGION_ }} run: yarn e2e diff --git a/examples/gatsby-starter-source-s3/gatsby-config.js b/examples/gatsby-starter-source-s3/gatsby-config.js index 3c01ebfd..388ca108 100644 --- a/examples/gatsby-starter-source-s3/gatsby-config.js +++ b/examples/gatsby-starter-source-s3/gatsby-config.js @@ -9,9 +9,9 @@ module.exports = { resolve: `@robinmetral/gatsby-source-s3`, options: { aws: { - accessKeyId: process.env.AWS_ACCESS_KEY_ID, - secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, - region: process.env.AWS_REGION, + accessKeyId: process.env.AWS_ACCESS_KEY_ID_, + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY_, + region: process.env.AWS_REGION_, }, buckets: [ "gatsby-source-s3-example", diff --git a/src/gatsby-node.ts b/src/gatsby-node.ts index d5111207..382c0a43 100644 --- a/src/gatsby-node.ts +++ b/src/gatsby-node.ts @@ -1,8 +1,6 @@ import { createRemoteFileNode } from "gatsby-source-filesystem"; import AWS = require("aws-sdk"); -const s3 = new AWS.S3(); - const isImage = (key: string): boolean => /\.(jpe?g|png|webp|tiff?)$/i.test(key); @@ -26,6 +24,7 @@ export async function sourceNodes( // configure aws AWS.config.update(awsConfig); + const s3 = new AWS.S3(); // get objects const getS3ListObjects = async (params: { @@ -87,10 +86,19 @@ export async function sourceNodes( // create file nodes objects?.forEach(async (object) => { + const { Bucket, Key } = object; + // get pre-signed URL + const url = s3.getSignedUrl("getObject", { + Bucket, + Key, + Expires: 60, + }); + createNode({ ...object, + url, // node meta - id: createNodeId(`s3-object-${object.Key}`), + id: createNodeId(`s3-object-${Key}`), parent: null, children: [], internal: { @@ -115,16 +123,9 @@ export async function onCreateNode({ }) { if (node.internal.type === "S3Object" && node.Key && isImage(node.Key)) { try { - // get pre-signed URL - const url = s3.getSignedUrl("getObject", { - Bucket: node.Bucket, - Key: node.Key, - Expires: 60, - }); - // download image file and save as node const imageFile = await createRemoteFileNode({ - url, + url: node.url, parentNodeId: node.id, store, cache,