How to connect in django s3 files storage from yandexcloud?

1.9k Views Asked by At
1

There are 1 best solutions below

0
Ryabchenko Alexander On BEST ANSWER

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())
    ...