Giter Site home page Giter Site logo

anathematic / react-s3-uploader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from odysseyscience/react-s3-uploader

0.0 1.0 1.0 128 KB

React component that renders an <input type="file"/> and automatically uploads to an S3 bucket

License: MIT License

JavaScript 100.00%

react-s3-uploader's Introduction

react-s3-uploader

Provides a React component that automatically uploads to an S3 Bucket.

Install

$ npm install react-s3-uploader

From Browser

var ReactS3Uploader = require('react-s3-uploader');

...

<ReactS3Uploader
    signingUrl="/s3/sign"
    accept="image/*"
    onProgress={this.onUploadProgress}
    onError={this.onUploadError}
    onFinish={this.onUploadFinish}/>

The above example shows all supported props.

This expects a request to /s3/sign to return JSON with a signedUrl property that can be used to PUT the file in S3.

The resulting DOM is essentially:

<input type="file" onChange={this.uploadFile} />

When a file is chosen, it will immediately be uploaded to S3. You can listen for progress (and create a status bar, for example) by providing an onProgress function to the component.

Server-Side

Bundled router

You can use the Express router that is bundled with this module to answer calls to /s3/sign

app.use('/s3', require('react-s3-uploader/s3router')({
    bucket: "MyS3Bucket"
}));

This also provides another endpoint: GET /s3/img/(.*). This will create a temporary URL that provides access to the uploaded file (which are uploaded privately at the moment). The request is then redirected to the URL, so that the image is served to the client.

Access/Secret Keys

The aws-sdk must be configured with your account's Access Key and Secret Access Key. There are a number of ways to provide these, but setting up environment variables is the quickest. You just have to configure environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, and AWS automatically picks them up.

Other Types of Servers

Boto for Python, in a Django project
import boto
import mimetypes
import json

...
conn = boto.connect_s3('AWS_KEY', 'AWS_SECRET')

def sign_s3_upload(request):
    object_name = request.GET['objectName']
    content_type = mimetypes.guess_type(object_name)[0]

    signed_url = conn.generate_url(
        300, 
        "PUT", 
        'BUCKET_NAME', 
        'FOLDER_NAME' + object_name,
        headers = {'Content-Type': content_type, 'x-amz-acl':'public-read'})

    return HttpResponse(json.dumps({'signedUrl': signed_url}))
Other Servers

If you do some work on another server, and would love to contribute documentation, please send us a PR!

react-s3-uploader's People

Contributors

angkec avatar jakerichan avatar mndvns avatar seanadkinson avatar shalstvedt avatar zeke avatar

Watchers

 avatar

Forkers

easil

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.