Ryabchenko Alexander
Ryabchenko Alexander

Reputation: 12470

How to connect in django s3 files storage from yandexcloud?

There are s3 from yandex cloud https://cloud.yandex.com/docs/storage/tools/?utm_source=console&utm_medium=empty-page&utm_campaign=storage

How kan I configure django to use it ?

Upvotes: 1

Views: 2371

Answers (1)

Ryabchenko Alexander
Ryabchenko Alexander

Reputation: 12470

I) Install boto3 and django-storages libs.

II) Add yandex_s3_storage.py file with the code below:

from storages.backends.s3boto3 import S3Boto3Storage

from sites.crm.settings import YOUR_YANDEX_BUCKET_NAME


class ClientDocsStorage(S3Boto3Storage):
    bucket_name = YANDEX_CLIENT_DOCS_BUCKET_NAME
    file_overwrite = False

III) Add the code below to settings.py:

INSTALLED_APPS = [
    ...
    'storages',
    ...
]

...

# ----Yandex s3----
DEFAULT_FILE_STORAGE = 'yandex_s3_storage.ClientDocsStorage'  # path to file we created before
YANDEX_CLIENT_DOCS_BUCKET_NAME = 'client-docs'
AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_S3_ENDPOINT_URL = 'https://storage.yandexcloud.net'
AWS_S3_REGION_NAME = 'storage'

IV) Add a file field to your model:

from sites.yandex_s3_storage import ClientDocsStorage

class ClientDocs(models.Model):
    ... 
    upload = models.FileField(storage=ClientDocsStorage())
    ... 

Upvotes: 6

Related Questions