简体   繁体   English

如何从 yandexcloud 连接 django s3 文件存储?

[英]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有来自yandex cloud https://cloud.yandex.com/docs/storage/tools/?utm_source=console&utm_medium=empty-page&utm_campaign=storage的s3

How kan I configure django to use it?我如何配置 django 来使用它?

I) install boto3 anf django-storages libs I) 安装boto3 django-storages storages 库

II) Add yandex_s3_storage.py file with next code II) 使用下一个代码添加yandex_s3_storage.py文件

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) in settings add next code III)在设置中添加下一个代码

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 file field to your model IV)将文件字段添加到您的 model

from sites.yandex_s3_storage import ClientDocsStorage

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM