Search code examples
node.jsamazon-web-servicesamazon-s3sdk

Getting 'Could not load credentials from any providers' error while using AWS-SDK in node.js


I am writing a code to copy a file from one to another and here is the code

Here i am using the code to first find if a file exists and if doesnt it makes one by copying files from one to the other

const AWS = require('aws-sdk');

const s3 = new AWS.S3({
    accessKeyId:  process.env.ACCESS_KEY,
    sercetAccessKey: process.env.SECRET_ACCESS_KEY,
    // region: "ap-south-1",
    endpoint: process.env.S3_ENDPOINT,
});

// console.log(process.env.S3_BUCKET)

async function copyS3Folder(sourcePrefix, destinationPrefix, continuationToken) {
    try {
        // List all objects in the source folder
        const listParams = {
            
            // accessKeyId:  process.env.ACCESS_KEY,
            Bucket: process.env.S3_BUCKET || "",
            Prefix: sourcePrefix,
            ContinuationToken: continuationToken
        };

        const listedObjects = await s3.listObjectsV2(listParams).promise();

        if (!listedObjects.Contents || listedObjects.Contents.length === 0) return;
        
        // Copy each object to the new location
        await Promise.all(listedObjects.Contents.map(async (object) => {
            if (!object.Key) return;
            let destinationKey = object.Key.replace(sourcePrefix, destinationPrefix);
            let copyParams = {
                Bucket: process.env.S3_BUCKET || "",
                CopySource: `${process.env.S3_BUCKET}/${object.Key}`,
                Key: destinationKey
            };

            console.log(copyParams);

            await s3.copyObject(copyParams).promise();
            console.log(`Copied ${object.Key} to ${destinationKey}`);
        }));

        // Check if the list was truncated and continue copying if necessary
        if (listedObjects.IsTruncated) {
            listParams.ContinuationToken = listedObjects.NextContinuationToken;
            await copyS3Folder(sourcePrefix, destinationPrefix, continuationToken);
        }
    } catch (error) {
        console.error('Error copying folder:', error);
    }
}

But I am getting this error

copying folder: Error [CredentialsError]: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
    at Timeout.connectTimeout [as _onTimeout] (D:\Coding\VStart Main\server\node_modules\aws-sdk\lib\http\node.js:70:15)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7) {
  code: 'CredentialsError',
  time: 2024-07-12T13:08:19.491Z,
  retryable: true,
  originalError: {
    message: 'Could not load credentials from any providers',
    code: 'CredentialsError',
    time: 2024-07-12T13:08:19.491Z,
    retryable: true,
    originalError: {
      message: 'EC2 Metadata roleName request returned error',
      code: 'TimeoutError',
      time: 2024-07-12T13:08:19.491Z,
      retryable: true,
      originalError: [Object]

I tried putting AccesKey and Passcode id in the params of the function but it didnt realise that and gave me an error for it...


Solution

  • You actually have a simple typo :).

    Change sercetAccessKey to secretAccessKey. Probably will work fine after that.