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

Commit 36a7e1d

Browse files
authored
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
1 parent d800faa commit 36a7e1d

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
run: yarn lint
1818
- name: E2E
1919
env:
20-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
21-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
22-
AWS_REGION: ${{ secrets.AWS_REGION }}
20+
AWS_ACCESS_KEY_ID_: ${{ secrets.AWS_ACCESS_KEY_ID_ }}
21+
AWS_SECRET_ACCESS_KEY_: ${{ secrets.AWS_SECRET_ACCESS_KEY_ }}
22+
AWS_REGION_: ${{ secrets.AWS_REGION_ }}
2323
run: yarn e2e

examples/gatsby-starter-source-s3/gatsby-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ module.exports = {
99
resolve: `@robinmetral/gatsby-source-s3`,
1010
options: {
1111
aws: {
12-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
13-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
14-
region: process.env.AWS_REGION,
12+
accessKeyId: process.env.AWS_ACCESS_KEY_ID_,
13+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY_,
14+
region: process.env.AWS_REGION_,
1515
},
1616
buckets: [
1717
"gatsby-source-s3-example",

src/gatsby-node.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { createRemoteFileNode } from "gatsby-source-filesystem";
22
import AWS = require("aws-sdk");
33

4-
const s3 = new AWS.S3();
5-
64
const isImage = (key: string): boolean =>
75
/\.(jpe?g|png|webp|tiff?)$/i.test(key);
86

@@ -26,6 +24,7 @@ export async function sourceNodes(
2624

2725
// configure aws
2826
AWS.config.update(awsConfig);
27+
const s3 = new AWS.S3();
2928

3029
// get objects
3130
const getS3ListObjects = async (params: {
@@ -87,10 +86,19 @@ export async function sourceNodes(
8786

8887
// create file nodes
8988
objects?.forEach(async (object) => {
89+
const { Bucket, Key } = object;
90+
// get pre-signed URL
91+
const url = s3.getSignedUrl("getObject", {
92+
Bucket,
93+
Key,
94+
Expires: 60,
95+
});
96+
9097
createNode({
9198
...object,
99+
url,
92100
// node meta
93-
id: createNodeId(`s3-object-${object.Key}`),
101+
id: createNodeId(`s3-object-${Key}`),
94102
parent: null,
95103
children: [],
96104
internal: {
@@ -115,16 +123,9 @@ export async function onCreateNode({
115123
}) {
116124
if (node.internal.type === "S3Object" && node.Key && isImage(node.Key)) {
117125
try {
118-
// get pre-signed URL
119-
const url = s3.getSignedUrl("getObject", {
120-
Bucket: node.Bucket,
121-
Key: node.Key,
122-
Expires: 60,
123-
});
124-
125126
// download image file and save as node
126127
const imageFile = await createRemoteFileNode({
127-
url,
128+
url: node.url,
128129
parentNodeId: node.id,
129130
store,
130131
cache,

0 commit comments

Comments
 (0)