【问题标题】:How to connect in django s3 files storage from yandexcloud?如何从 yandexcloud 连接 django s3 文件存储?
【发布时间】:2021-04-09 13:35:19
【问题描述】:

【问题讨论】:

    标签: django amazon-s3 django-storage yandexcloud


    【解决方案1】:

    我)安装boto3 anf django-storages libs

    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) 在设置中添加下一个代码

    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) 向模型添加文件字段

    from sites.yandex_s3_storage import ClientDocsStorage
    
    class ClientDocs(models.Model):
        ... 
        upload = models.FileField(storage=ClientDocsStorage())
        ... 
    

    【讨论】:

      猜你喜欢
      • 2020-02-26
      • 2017-06-14
      • 2020-02-24
      • 1970-01-01
      • 2020-01-27
      • 2023-02-17
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多