aws / aws-sdk-js-v3

Modularized AWS SDK for JavaScript.
Apache License 2.0
3.05k stars 574 forks source link

S3Client Instantiation not finding ENV variables #6211

Closed ChaoChow closed 2 months ago

ChaoChow commented 3 months ago

Checkboxes for prior research

Describe the bug

When instantiating the S3Client object on a Nodejs project with an empty configuration object, the S3Client fails to pull AWS credentials stored as env variables despite the documentation stating that the credential provider chain searches in multiple locations for credentials.

I found that instantiating the S3Client object bare like the below line does not fetch credentials properly const client = new S3Client({});

However, instantiating the S3Client object with the fromEnv() function does result in credentials being fetched properly from env variables. const client = new S3Client({credentials: fromEnv()});

SDK version number

@aws-sdk/client-s3@3.588.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.11.1

Reproduction Steps

  1. Save the GetObject example js code from https://docs.aws.amazon.com/AmazonS3/latest/userguide/example_s3_GetObject_section.html locally
  2. Change the bucket to your own bucket your AWS account
  3. Set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN as environment variables in terminal from your AWS login
  4. in the same terminal window run the example js code script using node
  5. See exception for invalid credentials
  6. Edit the example js code line 3 to the following: const client = new S3Client({credentials: fromEnv()}); you will need to npm install @aws-sdk/credential-provider-env and import fromEnv iin order to resolve fromEnv()

Observed Behavior

Running the script results in an invalid credentials error

Expected Behavior

Running the script fetches the specified file

Possible Solution

credential provider chain not searching for env variables first OR is unable to fetch env variables

Additional Information/Context

Running on ARM Mac OS

aBurmeseDev commented 3 months ago

Hi @ChaoChow - thanks for reaching out.

I followed your repro steps and was able to successfully make the API call reading credentials from environment variables. It's strange that you can use credential-providers to read your variables but Can you please verify the variables are being exported and loaded correctly? Sometimes you may need to restart the IDE since if the variables were set after you launched your IDE, they will not be propagated to that process until a restart happens. You can also validate if the variables are being loaded by node process by doing the following before instancing the client:

const varsSet = process.env.AWS_SECRET_ACCESS_KEY && process.env.AWS_ACCESS_KEY_ID;
console.log(varsSet ? "Variables are set" : "Variables are not set!");

As you've already mentioned, you can also use one of our @aws-sdk/credential-providers methods fromEnv to read the credentials from your environment variables.

Let me know if that helps or there's any further questions! Best, John

github-actions[bot] commented 2 months ago

This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.

github-actions[bot] commented 2 months ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.