Reputation: 390
I am trying to create a simple app that uploads an image from a web form to a S3 bucket. The problem I am having is with accessing the Access keys in my app. Initially, I was using aws-sdk v2 and was trying to read the environment variables stored in a .env file:
// Configure AWS SDK
AWS.config.update({
accessKeyId: process.env.MY_ACCESS_KEY_ID!,
secretAccessKey: process.env.MY_SECRET_ACCESS_KEY!,
region: "us-west-1",
});
I am unsure why I keep getting a "Missing Credentials" error. I replaced the process.env.* with the actual access keys and the app worked. However, I can't push this code since I must hide my access keys.
Next, I then tried aws-sdk v3 and used fromEnv():
const client = new S3Client({ region: "us-west-1", credentials: fromEnv() });
However, this gives the same error.
My goal is to run this locally and then upload this to AWS Amplify or some other hosting platform and then read the environment variables from there as well. How do I accomplish this?
EDIT: I even tried different approaches like using .env.local vs .env files. Creating an environment.d.ts file with types for the environment variables. Using dotenv. But nothing works.
Upvotes: -1
Views: 611
Reputation: 159
You need to require and configure dotenv
to load env variables from a .env
file into your app
require('dotenv').config();
For AWS SDK v3, you can use the fromEnv
method from @aws-sdk/credential-provider-node
to load credentials from env variables
import { S3Client } from '@aws-sdk/client-s3';
import { fromEnv } from '@aws-sdk/credential-provider-node';
const client = new S3Client({ region: 'us-west-1', credentials: fromEnv() });
Upvotes: 0