Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: use plugin options for AWS config (#45)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
robinmetral authored May 22, 2020
1 parent d800faa commit 36a7e1d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions examples/gatsby-starter-source-s3/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 12 additions & 11 deletions src/gatsby-node.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
Expand Down

0 comments on commit 36a7e1d

Please sign in to comment.