aws-sdk-js: CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Confirm by changing [ ] to [x] below to ensure that it’s a bug:

Describe the bug

I use AWS javascript SDK for enabling my web app clients upload their files to my s3 bucket. But for some client the “managed upload” getting failed (not always but often). Could you please help me debug this, as i am sure i correctly followed all steps mentioned in docs. Please see below for details:

ERROR REPORT

CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
    at r (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:78:2417 )
    at constructor.getCredentials (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:78:2945 )
    at constructor.<anonymous> (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:79:3316 )
    at constructor.callListeners (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:81:6740 )
    at constructor.emit (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:81:6480 )
    at constructor.emitEvent (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:80:24217 )
    at constructor.e (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:80:19827 )
    at i.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:82:30056 )
    at constructor.runTo (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:80:21515 )
    at constructor.send (https://sdk.amazonaws.com/js/aws-sdk-2.818.0.min.js?_=1615666287497:80:21408 )


SDK script used : https://sdk.amazonaws.com/js/aws-sdk-2.864.0.min.js

AWS initialization Code :

function initializeS3() {
	var albumBucketName = aws_details.bucket_name;
	var bucketRegion = aws_details.bucket_region;
	var IdentityPoolId = aws_details.identity_pool_id;
	var IdentityPoolRegion = aws_details.identity_pool_region;
	$("#upload-box").draggable();
	if(typeof(AWS) != 'undefined') {
		console.log("initializing s3 : done");
		AWS.config.region = bucketRegion;
		AWS.config.update({
			region: IdentityPoolRegion,
			credentials: new AWS.CognitoIdentityCredentials({
				IdentityPoolId: IdentityPoolId
			})
		});
		globalBucket = new AWS.S3({
			apiVersion: '2006-03-01',
			params: {Bucket: albumBucketName},
			httpOptions: {timeout: 1800000}
		});
	}
	else {
		console.log("initializing s3 : redo");
		setTimeout(initializeS3, 1000);
	}
}

UPLAOD CODE:

function process_uq(startingIndex, endingIndex) {
	for(current_index = startingIndex; current_index <= endingIndex; current_index++) {
		var current_item = global_upload_queue[current_index];
		var total_file_size = current_item.size;	
		
		var params = {
			Bucket: aws_details.bucket_name,
			Key: current_item.aws_key,
			Body: current_item.file,
			ACL: "public-read"
		};
		
		global_ongoing_xhr_count++;
		global_upload_queue[current_index]['upload_instance'] = new AWS.S3.ManagedUpload({queueSize : 3, partSize : const_min_chunk_size, params : params});
		global_upload_queue[current_index]['upload_instance'].on('httpUploadProgress', function(evt) {
			var local_uploadPartParams = this.service.config.params;
			//for getting queue index from upload id
			for(xyz in global_upload_queue) {
				if(global_upload_queue[xyz]['aws_key'] == local_uploadPartParams['Key']) {
					var local_index = xyz;
				}
			}
			//global_upload_queue[local_index]['total_size_uploaded'] += evt.loaded;

			showProgressBarMulti({
				uq_index : local_index,
				loaded : evt.loaded
			});
		});

		var promise = global_upload_queue[current_index]['upload_instance'].promise();

		promise.then(
			function(data) {
				global_ongoing_xhr_count--;
				//for getting queue index from upload id
				for(xyz in global_upload_queue) {
					if(global_upload_queue[xyz]['aws_key'] == data.Key) {
						local_index = xyz;
					}
				}			
				var index = local_index;
				completeMultiPartUpload(index);
			},
			function(err) {
				console.log("retrying : process_uq(" + startingIndex + ", " + endingIndex + ")");
				if(!global_upload_cancelled) {
					setTimeout(function() {
						process_uq(startingIndex, endingIndex);
						sendErrorLogToServer(err.stack);
					}, 10000);
				}
			}
		);
	}
}

Is the issue in the browser/Node.js? Browser

SDK version number https://sdk.amazonaws.com/js/aws-sdk-2.864.0.min.js

To Reproduce (observed behavior) It is sometime working and sometime failing

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 19 (1 by maintainers)

Most upvoted comments

Same problem

So in my case, the error is misleading. I looked at where it was thrown and it’s actually the region and a totally different message (that then gets rewritten somehow). If it points to this code:

    add('VALIDATE_REGION', 'validate', function VALIDATE_REGION(req) {
      if (!req.service.isGlobalEndpoint) {
        var dnsHostRegex = new RegExp(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9])$/);
        if (!req.service.config.region) {
          req.response.error = AWS.util.error(new Error(),
            {code: 'ConfigError', message: 'Missing region in config'}); // <<< *exception points to here*
        } else if (!dnsHostRegex.test(req.service.config.region)) {
          req.response.error = AWS.util.error(new Error(),
            {code: 'ConfigError', message: 'Invalid region in config'});
        }
      }
    });

Then this is a bug in the service constructor (in my case AWS.Location) and documentation that suggests region can be filled with a string. It may be, but it also has to be set in AWS.config.region. Working example code:

      AWS.config.region = 'eu-central-1'
      const credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'eu-central-1:your-id-here',
      })
      const locationClient = new AWS.Location({
        credentials,
      })

Hi there, have you tried explicitly setting the credentials in the constructor, I suspect that they get overwritten when the S3 client is initialized.

Basically doing something like:

var s3 = new aws.S3({region: 'us-west-2', credentials: creds});

May also wanna check if they are getting set with using something like:

console.log(s3.config.credentials)

Also can you point out what documentation you followed?

same issue : CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

exactly the same the error

Same problem here

Experiencing a similar challenge to this myself.

@vinithisonline did you solve it?