AWS Developer Tools Blog

React Native Support in the AWS SDK for JavaScript

We’re excited to announce React Native support in the AWS SDK for JavaScript.

You can now access all services that are currently supported in the AWS SDK for JavaScript from within a React Native application. You can configure Amazon Cognito Identity as the authentication provider by using the same Amazon Cognito Identity credentials you might already be familiar with from the browser SDK.

Getting Started with the SDK in React Native

Save the AWS SDK for JavaScript as a project dependency by running the following:

npm install --save aws-sdk

Within your project, import the SDK with the following example:

import AWS from 'aws-sdk/dist/aws-sdk-react-native';

Once imported, you can use the SDK the same way you use it in the browser. For example, the following shows how to get a list of folders from an S3 bucket, given a prefix:

import AWS from 'aws-sdk/dist/aws-sdk-react-native';

const s3 = new AWS.S3({
  region: 'REGION',
  credentials: {/* */}
});

export async function getFoldersByPrefix(bucket, prefix) {
  let nextMarker = null;
  let isTruncated = false;
  const folders = [];

  // Returns all objects in current 'directory'
  do {
    const s3Objects = await s3.listObjects({
      Bucket: bucket,
      Prefix: prefix || '',
      Delimiter: '/',
      Marker: nextMarker
    }).promise();
    // Store the folder paths
    const prefixes = s3Objects.CommonPrefixes.map(common => common.Prefix);
    folders.push.apply(folders, prefixes)
    // Check if there are more objects in this directory
    isTruncated = s3Objects.IsTruncated;
    nextMarker = isTruncated ? s3Objects.NextMarker : null;
  } while (isTruncated);
  
  return folders;
}

Take a look at the AWS SDK for JavaScript SDK Developer Guide for examples of how to make service calls with the SDK.

Give It a Try!

We look forward to hearing what you think of this new support for React Native in the AWS SDK for JavaScript! Try it out and leave your feedback in the comments or on GitHub!