Use Node js @aws-sdk/client-s3 Using AWS CLI Credentials from .env Only – Javascript

by
Ali Hasan
amazon-s3 amazon-web-services node-modules node.js

Quick Fix: To use AWS CLI credentials from .env with the @aws-sdk/client-s3 package, create a new S3Client object using the correct credentials properties extracted from the .env file.

The Problem:

A Node.js project uses the @aws-sdk/client-s3 module to interact with an S3 bucket. The project intends to utilize AWS credentials stored in a .env file, ensuring that the application exclusively uses these credentials for authentication. However, the current implementation inadvertently utilizes credentials from the local AWS CLI credentials file (~/.aws/credentials), leading to an error. The task is to modify the code to utilize AWS credentials solely from the .env file and configure it in S3Bucket.js to eliminate the conflict and ensure the application operates as intended.

Q&A

What is the correct way to use AWS credentials from .env in @aws-sdk/client-s3?

Use the credentials option in the client constructor.

Why is the code attempting to use credentials from ~/.aws/credentials?

The default credential provider chain checks ~/.aws/credentials for credentials.

Is there a way to use signature version 4 explicitly?

It’s not clear from the documentation, but you may be able to set signatureVersion to ‘v4’ in the client constructor options.

Video Explanation:

The following video, titled "NodeJs How to upload Files + uploading to AWS S3 using Express ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

This is a gold tutorial! I went through tons of tutorials on udemy and youtube on image uploading in node js, but none come even close to ...