Storage throws 'Error: Missing credentials in config' on any operation.
See original GitHub issueDescribe 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:
- Create a react native application
- Add AWS amplify
- Create a project on Mobile Hub
- Configure AWS Amplify with
aws-exports.js
- Use AWS Amplify Storage to put an object in S3
- 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:
- Created 5 years ago
- Reactions:8
- Comments:8 (2 by maintainers)
Top GitHub Comments
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.
I fixed it by setting the credentials hard with:
So this should be set in the library right?