Skip to content

File uploads - locally and on s3 #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Apr 5, 2022
Merged

File uploads - locally and on s3 #66

merged 36 commits into from
Apr 5, 2022

Conversation

kbadova
Copy link
Contributor

@kbadova kbadova commented Dec 6, 2021

Overview

This adds a files Django app, which enables both DIRECT and PASS-THRU upload scenarios, where the files can be uploaded both LOCAL and to S3.

The following combinations are possible:

  • Django admin + DIRECT + LOCAL
  • Django admin + DIRECT + S3
  • API + DIRECT + LOCAL
  • API + DIRECT + S3
  • API + PASS-THRU + LOCAL
  • API + PASS-THRU + S3

For the PASS-THRU, the API interface for the frontend remains the same - meaning, you don't need to write different code to handle LOCAL vs S3 on the frontend.

We can mix and match, depending on needs:

  1. Have Django admin uploading directly to S3.
  2. While having a pass-thru API for the frontend.
  3. And everything works with the same model / codebase.

For frontend testing, there's a separate PR here - https://github.com/HackSoftware/Example-React-Project/pull/1

Settings

This PR introduces few new settings.

The file upload style & storage are controlled via:

# Example .env file

FILE_UPLOAD_STRATEGY="pass-thru"
FILE_UPLOAD_STORAGE="s3"

Additionally, there are a bunch of AWS related settings, so django-storages and boto3 can do their job. They are located in config/settings/files_and_storages.py

Details

For S3 bucket configuration, you'll need the following CORS JSON:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "POST"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]

Or if you know what you're doing with your bucket - configure it yourself 👌

Presigned data returned from S3 looks like this:

{'fields': {'Content-Type': 'image/png',
            'acl': 'private',
            'key': 'files/bafdccb665a447468e237781154883b5.png',
            'policy': 'eyJleHBpcmF0aW9uIjogIjIwMjItMDQtMDVUMTE6NDk6MjJaIiwgImNvbmRpdGlvbnMiOiBbeyJhY2wiOiAicHJpdmF0ZSJ9LCB7IkNvbnRlbnQtVHlwZSI6ICJpbWFnZS9wbmcifSwgeyJidWNrZXQiOiAiZGphbmdvLXN0eWxlZ3VpZGUtZXhhbXBsZSJ9LCB7ImtleSI6ICJmaWxlcy9iYWZkY2NiNjY1YTQ0NzQ2OGUyMzc3ODExNTQ4ODNiNS5wbmcifSwgeyJ4LWFtei1hbGdvcml0aG0iOiAiQVdTNC1ITUFDLVNIQTI1NiJ9LCB7IngtYW16LWNyZWRlbnRpYWwiOiAiQUtJQVNPWkxaSTVGSkRKNlhUU1ovMjAyMjA0MDUvZXUtY2VudHJhbC0xL3MzL2F3czRfcmVxdWVzdCJ9LCB7IngtYW16LWRhdGUiOiAiMjAyMjA0MDVUMTE0OTEyWiJ9XX0=',
            'x-amz-algorithm': 'AWS4-HMAC-SHA256',
            'x-amz-credential': 'AKIASOZLZI5FJDJ6XTSZ/20220405/eu-central-1/s3/aws4_request',
            'x-amz-date': '20220405T114912Z',
            'x-amz-signature': '7d8be89aabec12b781d44b5b3f099d07be319b9a41d9a9c804bd1075e1ef5735'},
 'url': 'https://django-styleguide-example.s3.amazonaws.com/'}

Resources

@kbadova kbadova force-pushed the file-uploads branch 2 times, most recently from f9fd393 to 67eec36 Compare December 6, 2021 09:34
@kbadova kbadova force-pushed the file-uploads branch 2 times, most recently from ca69197 to 6f1f1fa Compare February 3, 2022 16:36
@kbadova kbadova force-pushed the file-uploads branch 8 times, most recently from e65e50d to bc2cdfe Compare March 4, 2022 12:59
@kbadova kbadova changed the title [WIP] Draft uploading files locally and on s3 File uploads - locally and on s3 Mar 4, 2022
@RadoRado
Copy link
Member

RadoRado commented Apr 3, 2022

👀

@RadoRado RadoRado merged commit d87863e into master Apr 5, 2022
@RadoRado RadoRado deleted the file-uploads branch April 5, 2022 12:04
@RadoRado RadoRado mentioned this pull request Apr 5, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants