question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storage throws 'Error: Missing credentials in config' on any operation.

See original GitHub issue

Describe the bug AWS.config.update({ credentials }); does not seem to set credentials for the S3 client.

AWS.config.update({
  region: region,
  credentials: credentials
});

To Reproduce Steps to reproduce the behavior:

  1. Create a react native application
  2. Add AWS amplify
  3. Create a project on Mobile Hub
  4. Configure AWS Amplify with aws-exports.js
  5. Use AWS Amplify Storage to put an object in S3
  6. Notice that the put fails and an error message is logged:

Error: Missing credentials in config

Expected behavior I expected to see the object in the S3 bucket and to receive no errors.

Smartphone (please complete the following information):

  • Device: iPhone 8
  • OS: iOS 11.4
  • React Native

Additional context I’m getting this with aws-amplify 1.1.1.

In my App.js, I’m doing this:

import Amplify, { Storage, Logger, Analytics, I18n } from "aws-amplify";
import aws_exports from "./src/aws-exports";
Amplify.configure(aws_exports);

When I try to put a file in the bucket, I get this:

[DEBUG] 03:55.298 StorageClass - put images/avatars/84ebf52f-0a2c-493d-9d92-6fb9f109facd.jpg to public/images/avatars/84ebf52f-0a2c-493d-9d92-6fb9f109facd.jpg
YellowBox.js:80 Object {[WARN] 03:55.307 StorageClass - error uploading: Error: Missing credentials in config
    at credError ...

I have added storage with awsmobile user-files configured:

╰─╼ $ awsmobile user-files enable

user-files is already enabled
# to configure the feature
    $ awsmobile user-files configure

I have the keys I’d expect to see in aws-exports.js.

    'aws_cognito_identity_pool_id': '[our pool id]',
    'aws_cognito_region': 'us-east-1',
    // ...
    'aws_user_files': 'enable',
    'aws_user_files_s3_bucket': '[our bucket]',
    'aws_user_files_s3_bucket_region': 'us-east-1',

I started digging into this issue a little deeper. If I put a breakpoint here in Storage.put I see that my credentials are set and credentialsOK is true. https://github.com/aws-amplify/amplify-js/blob/400b1cb2f393d33efe5aa824f730b3155a2f9822/packages/storage/src/Storage.ts#L155

When the S3 client gets created, AWS.config.credentials has an accessKeyId, identityId, secretAccessKey, and a sessionToken. https://github.com/aws-amplify/amplify-js/blob/400b1cb2f393d33efe5aa824f730b3155a2f9822/packages/storage/src/Storage.ts#L341-L352

But, when it gets to the aws-sdk-js, there are no credentials and no credential provider set so it errors out here: https://github.com/aws/aws-sdk-js/blob/fe88308a8699b39aef06492c898b265a3a24251f/lib/config.js#L367

I modified the code generated by Storage.ts locally to explicitly pass the credentials and everything worked as expected . . . now I’m not really sure how to tell what I’m doing wrong. 😕

    /**
     * @private
     */
    StorageClass.prototype._createS3 = function (options) {
        var bucket = options.bucket, region = options.region, credentials = options.credentials;
        core_1.AWS.config.update({
            region: region,
            credentials: credentials
        });
        return new S3({
            apiVersion: '2006-03-01',
            params: { Bucket: bucket },
            region: region,
            credentials: credentials // when I added this, things started working
        });
    };

My current workaround is to replace Storage._createS3 with another method that returns an S3 client with credentials set. For obvious reasons, I’d like to remove the workaround. 😃

// TODO: get rid of this stuff ASAP
import S3 from 'aws-sdk/clients/s3';

hackyCreateS3 = (options) => {
  const { bucket, region, credentials } = options;
  return new S3({
    apiVersion: '2006-03-01',
    params: { Bucket: bucket },
    region: region,
    credentials: credentials
  });
}

Storage._createS3 = hackyCreateS3;
// to here . . . this is all hacks

please don’t judge me 😦

At first, I thought this might be related to this issue: https://github.com/aws-amplify/amplify-js/issues/242

After further investigation, I’m pretty sure that it’s a different issue (potentially, I’m doing something dumb).

Thanks a lot for your help!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kevinsperrinecommented, Dec 12, 2018

FWIW, I determined this was caused by a devDependency pulling in a different version of the aws-sdk. After checking my yarn.lock, I found multiple versions of aws-sdk being included, and after I forced them to resolve to the version amplify was using the hacky workaround was no longer needed.

1reaction
bykofcommented, Dec 11, 2018

I fixed it by setting the credentials hard with:

import AWS from 'aws-sdk';
Amplify.configure(
        {
            Auth: {
                region: aws_config.cognito.REGION,
                userPoolId: aws_config.cognito.USER_POOL_ID,
                identityPoolId: aws_config.cognito.IDENTITY_POOL_ID,
                userPoolWebClientId: aws_config.cognito.APP_CLIENT_ID,
                mandatorySignIn: true,
            },
            Storage: {
                region: aws_config.s3.REGION,
                bucket: aws_config.s3.BUCKET,
                identityPoolId: aws_config.cognito.IDENTITY_POOL_ID,
            },
            API: {
                endpoints: [
                    {
                        name: aws_config.apiGateway.NAME,
                        endpoint: aws_config.apiGateway.URL,
                    }
                ]
            },
        }
    );

    AWS.config.credentials = Auth.essentialCredentials(await Auth.currentCredentials());

So this should be set in the library right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Missing credentials in config, if using AWS_CONFIG_FILE, set ...
When using credentials in plain text everything works good but when I am trying to use environment variables it's not working. Error message....
Read more >
Resolve "Unable to locate credentials" error in Amazon S3
An "Unable to locate credentials" error indicates that Amazon S3 can't find the credentials to authenticate AWS API calls. To resolve this issue,...
Read more >
AWS DynamoDB CredentialsError: Missing credentials in config
This is a common error in DynamoDB -- get the solution for AWS DynamoDB CredentialsError: Missing credentials in config.
Read more >
How do I fix the error "Unable to locate credentials ... - YouTube
How do I fix the error "Unable to locate credentials " when I connect to my S3 bucket with AWS CLI? · Chapters....
Read more >
Configuration and credential file settings - AWS Documentation
Storing credentials in the config file. You can keep all of your profile settings in a single file as the AWS CLI can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found