Home

Awesome

React Native AWS3

React Native AWS3 is a module for uploading files to S3. Unlike other libraries out there, there are no native dependencies.

npm install --save react-native-aws3

build status npm version npm downloads

Note on S3 user permissions

The user associated with the accessKey and secretKey you use must have the appropriate permissions assigned to them. My user's IAM policy looks like:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1458840156000",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectAcl",
                "s3:GetObjectVersion",
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:PutObjectVersionAcl"
            ],
            "Resource": [
                "arn:aws:s3:::my-bucket/uploads/*"
            ]
        }
    ]
}

Example

import { RNS3 } from 'react-native-aws3';

const file = {
  // `uri` can also be a file system path (i.e. file://)
  uri: "assets-library://asset/asset.PNG?id=655DBE66-8008-459C-9358-914E1FB532DD&ext=PNG",
  name: "image.png",
  type: "image/png"
}

const options = {
  keyPrefix: "uploads/",
  bucket: "your-bucket",
  region: "us-east-1",
  accessKey: "your-access-key",
  secretKey: "your-secret-key",
  successActionStatus: 201
}

RNS3.put(file, options).then(response => {
  if (response.status !== 201)
    throw new Error("Failed to upload image to S3");
  console.log(response.body);
  /**
   * {
   *   postResponse: {
   *     bucket: "your-bucket",
   *     etag : "9f620878e06d28774406017480a59fd4",
   *     key: "uploads/image.png",
   *     location: "https://your-bucket.s3.amazonaws.com/uploads%2Fimage.png"
   *   }
   * }
   */
});

Usage

put(file, options)

Upload a file to S3.

Arguments:

  1. file
  1. options

Returns an object that wraps an XMLHttpRequest instance and behaves like a promise, with the following additional methods:

Examples:

RNS3.put(file, options)
  .progress((e) => console.log(e.loaded / e.total)); // or console.log(e.percent)

RNS3.put(file, option)
  .abort();

TODO

License

MIT