Cole Perry
Cole Perry

Reputation: 160

Using AWS SDK in React Native - Missing credentials in config

I am trying to download an image that I have stored in an Amazon Web Services S3 Bucket in an Expo React Native application. I would include the documentation that I have used to do this but I have had to use such a large amalgamation of different docs that I can't locate them all.

I believe that I have all the correct info here and the code looks like it should work to me, but I am getting an unhandled promise rejection saying [Unhandled promise rejection: CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1] I will post the full message below.

This is what I am trying right now (I have substituted the identity pool, access key, and secret key but I am certain that they are all correct as I am using the access and secret key to upload the images elsewhere in the app):

  AWS.config.update({region: 'us-east-2'});
  AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: "MY-IDENTITY-POOL" }) //No idea if this is right
  const s3 = new AWS.S3({
    region: 'us-east-2',
    credentials: {accessKey: 'MY-ACCESS-KEY', secretKey: 'MY-SECRET-KEY',},
    params: {Bucket: 'rn-mobile-app-bucket'}
  });

  async function downloadFromS3 () {
    const file = await s3.getObject({ Bucket: 'rn-mobile-app-bucket', Key: 'Uploaded Photos/ColePic' }).promise();
    console.log(file);
    return {
     data: file.Body,
     mimetype: file.ContentType
    }
   }

And then I call the function in a useEffect:

  //Fetch all users from database
  useEffect(() =>{
    downloadFromS3();
    fetch('http://10.0.2.2:5000/forms').then(response =>{
      if(response.ok){
        return response.json();
      }
    }).then(data => setFormsArray(data));
  }, []);

You can ignore that fetch method it works fine and is mostly unrelated but I didn't want to omit code.

What am I missing here or what am I doing wrong?

Upvotes: 4

Views: 1468

Answers (1)

Wojciech Kozyra
Wojciech Kozyra

Reputation: 500

You should not use aws like that, if that was working and app was in the store, anyone could just download apk, unzip it and get full access to your aws account.

As for the actual implementation:

Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Clearly based on error message that library supports only node (or at least that part of functionality you are using). You can't have env variables on a phone and even if you would substitute that value in process.env manually that library would try to use fs package (from node standard library) which of course would not work in react-native

I don't know why you are trying to do it this way, to download image from aws s3 securely you need either:

  • make it public and just download it with fetch
  • setup http server that downloads that image from aws and sends it in response to user, but for this approach to make any sense you also need to have login/register because without any authentication this option is equivalent of making it public.

Upvotes: 2

Related Questions