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.