The credentials in AWS S3Client are failing to load correctly

I encountered an issue with the S3 Client from aws sdk v3:

When using the S3Client as outlined in the documentation and providing credentials via environment variables, I received an error message stating

The AWS Access Key Id you provided does not exist in our records.

Initially, I believed the problem was due to incorrect usage of AWS_ACCESS_KEY_ID. However, adding a specific line of code right after initializing the client resolved the error and correctly logged the values:

s3.config.credentials().then(console.log)

What puzzles me is that if I call this line within an asynchronous function or at any other point in the code, it no longer resolves the issue.

  • Why does calling this async function resolve subsequent executions?
  • Does it only temporarily fix the client, which remains instantiated for multiple function calls?
  • Could the promise be getting resolved too late – after the initial client call?
  • Why doesn't it work when called just before an s3 call (with or without await)?

Here's the relevant portion of my code:

const s3Config: S3ClientConfig = {}
s3Config.endpoint = new HttpRequest({...} as Endpoint) // used with a local s3 server
const s3 = new S3Client(s3Config);

// Workaround solution
s3.config.credentials().then(console.log)

export const upload = async (...) => {
    // Issue observed here
    // await s3.config.credentials().then(console.log)

    const streamUpload = new Upload({client: s3,...})
    return await streamUpload.done()
}


export const getTempLink = async (...) => {
    // Issue observed here
    // await s3.config.credentials().then(console.log)

    //* Get the pre-signed url
    const command = new GetObjectCommand({Bucket,Key})
    return await getSignedUrl(s3 as any, command as any, { expiresIn })
}

Thank you for your assistance!

Answer №1

I encountered a similar issue recently

The main issue was differentiating between aws-sdk and aws-sdk/client-s3

Upon investigating, I realized that the key names in the .env file were causing the problem:

ACCESS_KEY_ID,
SECRET_ACCESS_KEY,
AWS_SESSION_TOKEN

After making the necessary changes to the key names as shown below, everything started working smoothly with the credentials being recognized:

AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
AWS_SESSION_TOKEN,

It's important to keep in mind that AWS follows a chain of precedence for credentials

Answer №2

I have discovered some key information that helped me find answers to my questions:

Referencing the line in question, the code snippet that makes it work is

await s3.config.credentials()

What is the significance of this async function call in resolving the rest of the execution?

In response to this query, it was explained here that utilizing service constructors allows for multiple AWS configurations. Furthermore, as noted by Programmer89, the order of precedence matters, with a caveat:
AWS clients do not load the configuration immediately, but only when required.

By using the config.credentials() function, the configuration is loaded promptly.

Why does it not work when invoked just before an s3 call (with or without await)?

This configuration must be loaded immediately due to the usage of the same AWS SDK with other configurations (utilizing custom DynamoDB and S3 endpoints for local testing).

If called after any point, it appears to utilize the last environment variable used with that SDK rather than the one available at the time of constructor invocation.

Does this solely resolve the client temporarily? (the client remains instantiated for multiple function calls)

While uncertain, it may or may not, but this issue was observed only during local testing.

In production, these processes are better segregated. The client does not persist beyond a single call locally, hence no further investigation was conducted.

Is there a risk of the Promise resolving too late: subsequent to the initial client call?

It appears inconsequential as long as the client initiates loading the configuration (i.e., the Promise commences).

I trust that the insights gathered here could aid others facing similar challenges.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing jquery-confirm in CodeIgniter: A Step-by-Step Guide

I'm having some trouble with the jquery-confirm plugin from . Everything seems to be working fine, but when I try to delete an item, the jquery pops up an alert. However, when I click "okay", it doesn't actually delete the item. I can't figu ...

The dilemma between installing Electron or installing Electron-Builder: which one

When it comes to installing Electron for an Electron app with React, the method can vary depending on the tutorial. Some tutorials use electron-builder while others do not, but there is little explanation as to why. First: npx create-react-app app cd app ...

Ways to designate a parent element in Vue Draggable when the element is lacking a child

I'm currently incorporating vue-draggable into my project from the following GitHub repository: https://github.com/SortableJS/Vue.Draggable Below is my ElementsList component: <div> <draggable v-model="newElement" :move ...

Creative ways to use images as borders with CSS in React JS

import React, { Component } from 'react'; class BigText extends Component { constructor(props) { super(props); this.state = { title: '', text: '', summary: '' ...

Leveraging ES6 import declarations within Firebase functions

I've been experimenting with using ES6 in Firebase functions by trying to import modules like "import App from './src/App.js'. However, after adding type:"module" to my package.json, I encountered a strange error with Firebase ...

What causes the 'then' method of my angular service to return a resolved promise instead of the expected value?

I am perplexed as to why the "result" in this code snippet is a resolved promise instead of the actual value: searchService.getLink(vm.queryObject).then(function (result) { console.log(result); }); The implementation for the getLink() function is pro ...

Creating a Modal using Typescript with NextJS

Currently, I'm working on creating a modal within my app using NextJS with Typescript. Unfortunately, I've been struggling to eliminate the warning associated with my modal selector. Can someone provide guidance on how to properly type this? cons ...

Accessing an element by its ID with the help of a looping

Looking to retrieve a string from my database and insert it into my paragraphs: <p id="q_1"></p> <p id="q_2"></p> The following code works correctly: $.get("bewertung/get/1", function (data) { document.getElementById("q_1") ...

A guide on utilizing Stripe's payment_intent_data feature to automatically send an email to the customer following a successful transaction

I am struggling to send an email to the client after a successful payment. The documentation mentions setting "payment_intent_data.receipt_email", but my code below is not working as expected (no emails are being received). How should I properly configur ...

What causes the delay in CSS animations?

Is there a way to trigger an "updating" image to spin while a long JavaScript function is running? I am currently using JQuery to add a class called "spinning", defined in my CSS for the animation. The problem is that when I add the class and then call a l ...

Learn how to rearrange the order of Vue elements

I am working with a specific object structure in my code: lines: [{ order: '1', text: ' blue' },{ order: '2', text: 'green' },{ order: '3', text: 'yellow' }] Currently, thi ...

Opening a browser tab discreetly and extracting valuable data from it

Greetings to the experts in Chrome Extension development, I am exploring ways to access information from a webpage without actually opening it in a separate tab. Is there a method to achieve this? Here's the scenario: While browsing Site A, I come a ...

Issue with onClientClick not functioning properly when performing a jQuery function call

How can I make a jQuery form appear when an ASP.NET server-side button is clicked by the user? Currently, when I click on the button during runtime, the page reloads quickly without displaying the jQuery form. I am aiming to achieve a similar effect show ...

What could be causing FormArrayName to consistently display as undefined in my HTML code, even when the safe-navigation operator is employed

Currently, I'm referring to the Angular Material example found at https://angular.io/api/forms/FormArrayName Upon initializing the packageForm formGroup in ngOnInit() and logging it in the console during ngAfterViewInit(), I can see that the translat ...

The switch switches on yet another switch

Greetings everyone, Currently, I am in the midst of my exam project and creating a mobile menu. The functionality is there, but unfortunately, when closing the menu, it also triggers the search toggle which displays an unwanted div, becoming quite botherso ...

Tips for labeling subplots in PLOTLY JS

Looking for some guidance on adding titles to plots in Plotly JS. I've checked out the documentation but couldn't find anything helpful. Any tips or suggestions would be greatly appreciated! ...

Completely enlarge this inline-block CSS div scan-line

I am looking to create a unique scan line effect that gradually reveals text from left to right, mimicking the appearance of a cathode-ray burning text into a screen's phosphors. The concept involves sliding across black rows with transparent tips. Y ...

Dealing with multiple POST requests at once in Node Express JS - Best Practices

I've been working on a Node project with Express to handle incoming GET/POST requests. I have set up routes to manage various types of requests. One specific route, /api/twitter/search, calls a function that uses promises to retrieve Twitter feeds and ...

Having trouble receiving a complete response when making an ajax request to fetch an entire webpage

Currently, I am experimenting with J-Query Ajax functionality. My goal is to send a request in order to retrieve the entire HTML data of a page that I have hosted on bitbaysolutions.com. The issue arises when I load this URL and execute document.getElement ...

Using regular expressions in JavaScript to extract the value following a colon, without including the colon itself

I've attempted using the tool, along with a similar stackoverflow question, but I'm still unable to solve or comprehend it. I hope someone here can shed some light on what I might be missing. I've provided as detailed and step-by-step examp ...