Transitioning code from gatsby to nextjs has caused an issue with the aws-sdk integration

I have successfully integrated the aws-sdk with Gatsby by using a .env file to upload a file to an S3 bucket. Now, I am in the process of migrating this functionality to Next.js, but I'm encountering an error in Next.js.

Uncaught (in promise) CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Although I can see that the access id and key are properly set from the .env.local file when I console log the s3 variable, the upload function is failing. Below is the code I'm using in both Next.js and Gatsby.

const s3 = new AWS.S3({
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    region: process.env.AWS_REGION,
})

// console.log('s3:', s3)

        
useImperativeHandle(ref, () => ({
    async upload() {
        const params = {
            Bucket: 'ntcasting',
            Key: `${project}-${basecampprojectid}/${surname}${firstname.charAt(0)}/${surname}${firstname.charAt(0)}-decform-${filedate}.${fileExtension}`,
            Body: selectedfiles,
            Tagging: `basecamp_project_id=${basecampprojectid}`,
        }

        await s3
            .upload(params)
            .on('httpUploadProgress', (progressEvent, response) => {
                const percent = Math.floor((progressEvent.loaded / progressEvent.total) * 100)
                setuploadprog(percent)
                console.log(percent)
            })
            .promise()

I would greatly appreciate any feedback on this issue.

Answer №1

Hey there! You can give this code a shot in your next.config.js file to see if it can assist you:

 config.plugins.push(new webpack.DefinePlugin({
        'process.env': {
            AWS_ACCESS_KEY_ID: JSON.stringify(process.env.AWS_ACCESS_KEY_ID),
        }
    }))

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

The Discord.js collector seems to have no intention of ending, completely disregarding its keys in order

Help Needed with Discord Message Collector Issue I encountered a problem with my code that assigns ranks on Discord. It seems that the message collector is not responding to keys that should stop it from collecting messages. Can anyone offer assistance in ...

"Exploring the versatility of Tailwind css: A guide to implementing multiple themes

I know how to customize dark and light themes in tailwind, but I'm curious about extending this functionality to include additional themes such as pink, blue, red, etc. Is there a way to achieve this with regular tailwind CSS? <p className="t ...

Creating an angular controller in a template based on certain conditions

When working with Angular.js, I'm attempting the following: index.html <div class="data-tabs-sms-scroll" ng-show="question.type == 'open'" ng-controller="AudioMessagesCtrl" ng-include="'/templates/audioMessages.html' ...

What is the best way to insert events into your Google Calendar directly from a website?

Looking for some help with integrating Google Calendar and adding event features to my HTML page. I've successfully embedded the calendar onto my website, but I'm struggling to add events directly from the webpage. Is there a way to make this fea ...

Converting a string into an array of JSON objects

I am currently attempting to send data to my NodeJS server using the HTTP protocol (vue-resource). The goal is to send an array of JSON objects structured like this : [{"name":"Charlotte","surname":"Chacha","birth":"2000-04-02"},{"name":"Michael","surname ...

Triggering a JavaScript event with every update to the DOM marked as "completed"

I've been having some trouble with a specific task. I'm working on a script where I need to execute some functions after DOM manipulation is complete. The challenge is that I can't make changes to any other scripts on the page, as they might ...

What is the best way to access data from this $scope in AngularJS?

Upon printing selecteditems to the console, this is the output: [{"model":"Lumia","brand":"Nokia","subModel":["Lumia 735 TS","Lumia 510"],"city":"Bangalore"}] I have stored it in $scope.details as follows: var selecteditems = $location.search().items ...

Looking for a Search Field that clears default text when focused - Compatible with Firefox 3.6?

I have attempted various jQuery and JavaScript solutions found on this page: How to clear text field on focus of text field Surprisingly, all of the solutions work except for FF 3.6. So, I am wondering, What alternative can I use to make this feature com ...

Want to easily switch between pictures in the Gallery?

I've created a gallery using jQuery, CSS & HTML and now I want to implement next and previous image buttons. I have added the buttons to the page and written functions for them, but I am struggling with the implementation. Here is my code, if anyone h ...

Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images. First screenshot below shows that it works ...

Progress bar for AJAX file loading for Flash player

Looking to create a progress bar using AJAX for a flash file. Check out this demo here: I tried to analyze their page, but the JavaScript is encrypted and I'm not very skilled in JS. Any suggestions? Thank you! ...

When using multiple select tags with *ngFor in Angular, modifying the value of the first select tag will have an

<table id="DataTable" border="1" ALIGN="center"> <tr ALIGN="center"> <th>name</th> <th>address</th> <th>number</th> <th>type</th> </tr> <tr class="tcat" *ngFor ...

Although hiding and showing elements in jQuery is quick, the rendering engine is too sluggish

Experiencing a performance problem when toggling the visibility of all "tr.content" elements within a complex dom structure like this:    <tbody class="collapsible">        <tr class="handler">            <td>Collaps ...

jQuery conceal input field after it has been displayed

I've created an HTML form with two different sections for search purposes. The first section displays basic fields, and by clicking on "Advanced Search" (in my case, "Расширенный поиск"), additional fields are revealed. Everything work ...

Encasing the window global variable within an AngularJS framework

Currently, I am encapsulating a global variable within a factory in order to make it injectable. Here is an example of how it's done: angular.module('Analytics').factory('woopra', [ '$window', function ($window) ...

Exploring the concept of type identification in interpreted dynamic languages

How do dynamic scripting languages like Python, PHP, and JavaScript determine the datatype of a variable? /* C code */ int a = 1; int b = 2; int c = a * b; In the above C example, the compiler recognizes that 'a' and 'b' are integers. ...

Socketio: Issue: Isolated surrogate U+D83D is not a valid scalar value

I've been experiencing frequent crashes with my node.js server recently, all due to a recurring socket.io error. It seems that the client may be sending invalid UTF strings, causing an error in the utf8.js file. I'm getting frustrated with the co ...

Store the JSON file for future reference to extract additional information from it at a later time

I am struggling to update my JSON file with new data and retrieve that updated data when needed. Currently, whenever I try to access any information from the JSON file, it shows the same data as the last time I saved it manually. I have made some changes ...

Dropdown in Angular JS is displaying incorrect values in the options

I am a newcomer to AngularJS and encountering an issue with populating a dropdown. Within a foreach loop of data retrieved from the server, I have created an array of user data: $scope.Users.push( { userid: ...

Escape sequences do not seem to be functioning properly when using innerHTML

I am facing an issue where a string containing HTML escape characters (such as < and >) needs to be rendered inside a div using innerHTML. The intention is for the escaped characters to appear as text rather than as actual HTML, but they still render ...