okgrow/react-native-aws3

Name: react-native-aws3

Owner: OK GROW!

Description: Pure JavaScript React Native library for uploading to AWS S3

Created: 2018-04-18 18:45:32.0

Updated: 2018-04-18 19:00:00.0

Pushed: 2018-04-18 18:59:59.0

Homepage: null

Size: 74

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

React Native AWS3

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

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
rt { RNS3 } from 'react-native-aws3';

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


t options = {
yPrefix: "uploads/",
cket: "your-bucket",
gion: "us-east-1",
cessKey: "your-access-key",
cretKey: "your-secret-key",
ccessActionStatus: 201


.put(file, options).then(response => {
 (response.status !== 201)
throw new Error("Failed to upload image to S3");
nsole.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
  2. uri required - File system URI, can be assets library path or file:// path
  3. name required - The name of the file, will be stored as such in S3
  4. type required - The mime type, also used for Content-Type parameter in the S3 post policy
  5. options
  6. acl - The Access Control List of this object. Defaults to public-read
  7. keyPrefix - Prefix, or path to the file on S3, i.e. uploads/ (note the trailing slash)
  8. bucket required - Your S3 bucket
  9. region required - The region of your S3 bucket
  10. accessKey required - Your S3 AWSAccessKeyId
  11. secretKey required - Your S3 AWSSecretKey
  12. successActionStatus - HTTP response status if successful, defaults to 201
  13. awsUrl - AWS S3 url. Defaults to s3.amazonaws.com
  14. timeDelta - Devices time offset from world clock in milliseconds, defaults to 0

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

Examples:

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

.put(file, option)
bort();
TODO
License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.