I’m trying to use @aws-sdk/client-s3
to read and write some files to an S3 bucket. However, when trying to do this within SvelteKit I get this error:
CredentialsProviderError: Could not load credentials from any providers
at /repo-location/node_modules/@aws-sdk/credential-provider-node/dist-cjs/defaultProvider.js:13:11
This looks to be because it is looking for process.env
variables (among other places).
Is there a way for me to do something like this (I know the below is incorrect)?
import { S3Client } from '@aws-sdk/client-s3';
import { env } from '$env/dynamic/private';
const client = new S3Client({ env, region });
How can I get it to use SvelteKit’s way of handling environment variables instead? Or am I approaching this the wrong way?
2
Answers
Thanks to Filippo, I got the issue resolved. Here's the full TypeScript code for me to get it working.
I don’t see any problem with providing static credential values via .env files.
I’ve tried creating an S3 Client myself and loading the credentials from the .env, following this example in the Svelte docs.
Here’s my .env file, located at the root of the project:
Here’s how to create the S3 client using JS:
However, please note that in a real-world environment, you shouldn’t hardcode credentials.
Refer to the documentation to determine the most suitable method depending on where your code is running.