Skip to content

Hipo/yediemin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Yediemin

A package for bulletproof attachment serving in Django Rest Framework.

Getting Started

Requirements

  • Nginx
  • Django Rest Framework
  • Session Authentication
  • Django Storages (S3)

Installation Steps

  1. Install package from PyPi.
pip install yediemin
  1. Add the view to urls.py
from yediemin import YedieminView

urlpatterns = [
    re_path(r'^yediemin/(?P<file_name>\S+)/$', YedieminView.as_view(), name='yediemin'),
]
  1. Configure Nginx. Place the configuration below under your server.
location /yediemin-files/ {
            internal;
            resolver 8.8.8.8;
            set $redirect_uri "$upstream_http_redirect_uri";

            proxy_buffering off;
            proxy_pass $redirect_uri;
}
  1. Use YedieminFileField in serializer for FileField.
from yediemin import YedieminFileField

class AttachmentSerializer(serializers.ModelSerializer):
    file = YedieminFileField()

    class Meta:
        model = Attachment
        fields = (
            "id",
            "file",
        )
  1. Use PrivateS3Boto3Storage for the field in models.py
from yediemin import PrivateS3Boto3Storage

class Attachment(models.Model):
    file = models.FileField(storage=PrivateS3Boto3Storage())
  1. Upload files to S3 with YedieminFileField. Yediemin requires presigned object url.

  2. Make private already uploaded S3 objects by using AWS CLI. Change <bucket-name> and <bucket-name>/<path>/<to>/<folder> with your use-case.

# Script is inspired from 'Varun Chandak' (https://stackoverflow.com/a/48060930).

export AWS_PAGER="" # https://stackoverflow.com/a/60374670

aws s3 ls --recursive s3://<bucket-name>/<path>/<to>/<folder> | cut -d' ' -f5- | awk '{print $NF}' | while read line; do
    echo "$line"
    aws s3api put-object-acl --acl private --bucket <bucket-name> --key "$line"
done

Settings

  • YEDIEMIN_HIDDEN_REDIRECT_PATH

Default: yediemin-files. It should be same with location in nginx configuration.

  • YEDIEMIN_AUTHENTICATION_CLASSES

Default: [rest_framework.authentication.SessionAuthentication]

  • YEDIEMIN_EXPIRE_IN

Default: 604800 seconds which is 1 week. This is the maximum limit provided by AWS. Using Query Parameters

About

A package for bulletproof attachment serving in Django Rest Framework.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages