Django上传视屏 图片 到七牛云

况浩邈
2023-12-01

上传视屏 图片 到七牛云

  • 参考七牛云SDK

  • # pythonSDK
    https://developer.qiniu.com/kodo/sdk/1242/python
    
  • 在 oauth/urls.py 中添加生成七牛云token的路由2

  • urlpatterns = [
    path('qntoken/', views.QNYTokenView.as_view()), # /oauth/qntoken/
    ]
    
    
  • 在 oauth/views.py 中添加视图函数

  • '''获取上传token'''
    
    
    class QNYTokenView(APIView):
        def get(self, request):
            from qiniu import Auth, put_file, etag
            import qiniu.config
            # 需要填写你的 Access Key 和 Secret Key
            access_key = "x9IZ00wv3IyGX5H6eGj0ZXL3gRjE_mZJBQ2gANgv"
            secret_key = "Ms3mFWo-QFkHFvBoOSTttuA1iykrpDoeiseipNrN"
            # 构建鉴权对象
            q = Auth(access_key, secret_key)
            # 要上传的空间
            bucket_name = 'syl-1911a'
            # 生成上传 Token,可以指定过期时间等
            token = q.upload_token(bucket_name, expires=3600)
            return Response({'code': 0, 'msg': '获取', 'data': {'uptoken': token}})
    
    
  • 测试获取七牛云token接口

  • http://192.168.56.100:8888/oauth/qntoken/
    
  • {
        "code": 0,
        "msg": "获取",
        "data": {
            "uptoken": "x9IZ00wv3IyGX5H6eGj0ZXL3gRjE_mZJBQ2gANgv:_jyVEzLl96JIbcIqf4vtGi0-j1k=:eyJzY29wZSI6InN5bC0xOTExYSIsImRlYWRsaW5lIjoxNjA0ODM3NTM1fQ=="
        }
    }
    
  • 获取七牛云秘钥 1.2 在syl/settings.py中配置七牛云上传

  • 参考官方 syl/settings.py 中配置七牛云上传 https://portal.qiniu.com/user/key

  • 在syl/settings.py中配置七牛云上传

  • 参考官方
    syl/settings.py 中配置七牛云上传
    https://portal.qiniu.com/user/key
    https://github.com/glasslion/django-qiniu-storage
    
  • # 增加配置settings.py
    QINIU_ACCESS_KEY = "x9IZ00wv3IyGX5H6eGj0ZXL3gRjE_mZJBQ2gANgv" # 七牛
    QINIU_SECRET_KEY = "Ms3mFWo-QFkHFvBoOSTttuA1iykrpDoeiseipNrN" # 七牛
    QINIU_BUCKET_NAME = "syl-1911a" # 库名字
    QINIU_BUCKET_DOMAIN = "qjepbbvn7.hb-bkt.clouddn.com" # 外部访问链接
    # http://qjepbbvn7.hb-bkt.clouddn.com
    QINIU_SECURE_URL = False # 使用http
    PREFIX_URL = 'http://'
    MEDIA_URL = PREFIX_URL + QINIU_BUCKET_DOMAIN + '/media/'
    DEFAULT_FILE_STORAGE = 'qiniustorage.backends.QiniuMediaStorage' # 指定文件管理类
    MEDIA_ROOT = os.path.join('', "media")
    # MEDIA_URL = '/media/'
    # MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
    
  • 使用admin测试

 类似资料: