# django-s3file A lightweight file upload input for Django and Amazon S3. Django-S3File allows you to upload files directly AWS S3 effectively bypassing your application server. This allows you to avoid long running requests from large file uploads. This is particularly helpful for if you run your service on AWS Lambda or Heroku where you have a hard request limit. [![PyPi Version](https://img.shields.io/pypi/v/django-s3file.svg)](https://pypi.python.org/pypi/django-s3file/) [![Test Coverage](https://codecov.io/gh/codingjoe/django-s3file/branch/main/graph/badge.svg)](https://codecov.io/gh/codingjoe/django-s3file) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/codingjoe/django-s3file/main/LICENSE) ## Features - lightweight: less 200 lines - no JavaScript or Python dependencies (no jQuery) - easy integration - works just like the built-in - extendable JavaScript API ## For the Nerds ```mermaid sequenceDiagram autonumber actor Browser participant S3 participant Middleware Browser->>Django: GET form view activate Django Django->>Browser: RESPONSE w/ presigned POST URL & signed middleware key deactivate Django Browser->>S3: POST large file activate S3 S3->>Browser: RESPONSE AWS S3 key Browser->>Middleware: POST AWS S3 key (signed) activate Middleware Middleware->>S3: GET AWS S3 key S3->>Middleware: RESPONSE large file promise deactivate S3 Middleware->>Django: request incl. large file promise deactivate Middleware activate Django opt only if files is procssed by Django Django-->>S3: GET large file activate S3 S3-->>Django: RESPONSE large file deactivate S3 end Django->>Browser: RESPONSE success deactivate Django ``` In a nutshell, we can bypass Django completely and have AWS handle the upload or any processing. Of course, if you want to do something with your file in Django, you can do so, just like before, with the added advantage, that your file is served from within your datacenter. ## Installation Make sure you have [Amazon S3 storage](http://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html) setup correctly. Just install S3file using `pip`. ```bash pip install django-s3file # or pipenv install django-s3file ``` Add the S3File app and middleware in your settings: ```python # settings.py INSTALLED_APPS = ( '...', 's3file', '...', ) MIDDLEWARE = ( '...', 's3file.middleware.S3FileMiddleware', '...', ) ``` ## Usage S3File automatically replaces Django's `ClearableFileInput` widget, you do not need to alter your code at all. The `ClearableFileInput` widget is only than automatically replaced when the `DEFAULT_FILE_STORAGE` setting is set to `django-storages`' `S3Boto3Storage` or the dummy `FileSystemStorage` is enabled. ### Setting up the AWS S3 bucket #### Upload folder S3File uploads to a single folder. Files are later moved by Django when they are saved to the `upload_to` location. It is recommended to [setup expiration](http://docs.aws.amazon.com/AmazonS3/latest/dev/intro-lifecycle-rules.html) for that folder, to ensure that old and unused file uploads don't add up and produce costs. The default folder name is: `tmp/s3file` You can change it by changing the `S3FILE_UPLOAD_PATH` setting. #### CORS policy You will need to allow `POST` from all origins. Just add the following to your CORS policy. ```json [ { "AllowedHeaders": [ "*" ], "AllowedMethods": [ "POST" ], "AllowedOrigins": [ "*" ], "ExposeHeaders": [], "MaxAgeSeconds": 3000 } ] ``` ### Progress Bar S3File does emit progress signals that can be used to display some kind of progress bar. Signals named `progress` are emitted for both each individual file input as well as for the form as a whole. The progress signal carries the following details: ```javascript console.log(event.detail) { progress: 0.4725307607171312 // total upload progress of either a form or single input loaded: 1048576 // total upload progress of either a form or single input total: 2219064 // total bytes to upload currentFile: File {…} // file object currentFileName: "text.txt" // file name of the file currently uploaded currentFileProgress: 0.47227834703299176 // upload progress of that file originalEvent: ProgressEvent {…} // the original XHR onprogress event } ``` The following example implements a Boostrap progress bar for upload progress of an entire form. ```html