1.1.0 • Published 7 years ago

react-dropzone-s3-uploader v1.1.0

Weekly downloads
4,472
License
MIT
Repository
github
Last release
7 years ago

Drag and drop s3 file uploader for React

This component uploads files dropped into react-dropzone to s3 with react-s3-uploader.

For more detailed docs see the source packages

Usage (client)

Available props

s3Url and upload are the only props that require configuration. All others have sensible defaults that may be overridden.

PropTypeDescription
s3Urlstring.isRequiredThe url of your s3 bucket (https://my-bucket.s3.amazonaws.com)
uploadobject.isRequiredUpload options passed to react-s3-uploader. See react-s3-uploader for available options. Don't set onProgress, onError or onFinish here - use the ones below
filenamestringUsed as the default value if present. Filename of an image already hosted on s3 (i.e. one that was uploaded previously)
notDropzonePropsarrayA list of props to not pass to react-dropzone
isImagefuncA function that takes a filename and returns true if it's an image
imageComponentfuncComponent used to render an uploaded image
fileComponentfuncComponent used to render an uploaded file
progressComponentfuncComponent used to render upload progress
errorComponentfuncComponent used to render an error
childrennode || funcIf present the above components will be ignored in favour of the children
passChildrenPropsboolIf true we pass the current state to children of this component. Default is true. Set to false to avoid React warnings about unused props.
onDropfuncCalled when a file is dropped onto the uploader
onErrorfuncCalled when an upload error occurs
onProgressfuncCalled when a chunk has been uploaded
onFinishfuncCalled when a file has completed uploading. Called once per file if multi=true
...restAll other props are passed on to the react-dropzone component

Example

import DropzoneS3Uploader from 'react-dropzone-s3-uploader'

export default class S3Uploader extends React.Component {

  handleFinishedUpload = info => {
    console.log('File uploaded with filename', info.filename)
    console.log('Access it on s3 at', info.fileUrl)
  }

  render() {
    const uploadOptions = {
      server: 'http://localhost:4000',
      signingUrlQueryParams: {uploadType: 'avatar'},
    }
    const s3Url = 'https://my-bucket.s3.amazonaws.com'

    return (
      <DropzoneS3Uploader
        onFinish={this.handleFinishedUpload}
        s3Url={s3Url}
        maxSize={1024 * 1024 * 5}
        upload={uploadOptions}
      />
    )
  }
}

Custom display component

Specify your own component to display uploaded files. Passed a list of uploadedFile objects.

// elsewhere
class UploadDisplay extends React.Component {
  
  renderFileUpload = (uploadedFile, i) => {
    const {
      filename,   // s3 filename
      fileUrl,    // full s3 url of the file
      file,       // file descriptor from the upload
    } = uploadedFile

    return (
      <div key={i}>
        <img src={fileUrl} />
        <p>{file.name}</p>
      </div>
    )
  }

  render() {
    const {uploadedFiles, s3Url} = this.props
    return (
      <div> 
        {uploadedFiles.map(this.renderFileUpload)}
      </div>
    )
  }
}

// back in your uploader...
class S3Uploader extends React.Component {

  //...

  render() {
    return (
      <DropzoneS3Uploader 
        onFinish={this.handleFinishedUpload} 
        upload={uploadOptions}
      >
        <UploadDisplay />
      </DropzoneS3Uploader>
    )
  }
}

Usage (server)

  • Use s3Router from react-s3-uploader to get signed urls for uploads.
  • react-dropzone-s3-uploader/s3router can be used as an alias for react-s3-uploader/s3router.
  • See react-s3-uploader for more details.
app.use('/s3', require('react-dropzone-s3-uploader/s3router')({
  bucket: 'MyS3Bucket',                           // required
  region: 'us-east-1',                            // optional
  headers: {'Access-Control-Allow-Origin': '*'},  // optional
  ACL: 'private',                                 // this is the default - set to `public-read` to let anyone view uploads
}));
1.1.0

7 years ago

1.0.0

7 years ago

1.0.0-rc.3

7 years ago

1.0.0-rc.2

7 years ago

1.0.0-rc.1

7 years ago

0.11.0

7 years ago

0.10.0

7 years ago

0.9.0

7 years ago

0.8.1

7 years ago

0.8.0

7 years ago

0.7.3

8 years ago

0.7.2

8 years ago

0.7.1

8 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago