当前位置:网站首页>Flask 封装七牛云
Flask 封装七牛云
2022-07-26 17:38:00 【ZXY_lucky】
为何要封装?
我们在后端写视图时,时不时会用到上传图片到七牛云,为了节约时间,减少代码重用问题,把七牛云代码封装起来,使用时直接调用即可!
1.配置文件
把七牛所用的参数放在配置文件中,避免修改
2.封装
from qiniu import Auth,put_file
from flask import current_app
class QiniuTool():
def __init__(self):
ak = current_app.config.get('QINIU_AK')
sk = current_app.config.get('QINIU_SK')
self.q = Auth(ak,sk)
self.bucket_name = current_app.config.get('QINIU_BUCKET')
def upload(self,localfilepath,newfilename):
''' 七牛云上传图片 :param localfilepath: 本地文件地址 :param newfilename:上传后的文件名 :return: '''
token = self.q.upload_token(self.bucket_name,newfilename,3600)
res = put_file(token,newfilename,localfilepath,version='v2')
print(token)
if res[0]['key'] == newfilename:
return newfilename
else:
return False
边栏推荐
- [translation] Why do you need an API gateway to manage access to your API?
- 效率提升98%!高海拔光伏电站运维巡检背后的AI利器
- 剑指offer 连续子数组的最大和(二)
- SSH based online mall
- Netease game R & D Engineer Intern (client side)
- Leetcode 50 day question brushing plan (day 2 - the longest substring without repeated characters 10.00-12.00)
- .net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
- 神经网络学习(2)前言介绍二
- [ Kitex 源码解读 ] 服务发现
- PS_ 1_ Know the main interface_ New document (resolution)_ Open save (sequence animation)
猜你喜欢

SSM practice day 5

PS_1_认识主界面_新建文档(分辨率)_打开保存(序列动画)

LeetCode50天刷题计划(Day 1—— 两数相加 11.00-12.30)

Leetcode 50 day question brushing plan (day 5 - longest palindrome substring 10.50-13:00)

你适合做自动化 测试吗?

During the oppo interview, 16 questions were thrown over. I was stupid
![[kitex source code interpretation] service discovery](/img/70/c74ede02b794e586d629876d2b2376.png)
[kitex source code interpretation] service discovery

Sword finger offer regular expression matching

菜鸟 CPaaS 平台微服务治理实践

Redis master-slave replication, read-write separation, sentinel mode
随机推荐
ECS MySQL prompt error
mpc5744p的pit报错, RTOS无法启动, 时钟源问题
[a little knowledge] thread pool
骚操作:巧用MySQL主从复制延迟拯救误删数据
During the oppo interview, 16 questions were thrown over. I was stupid
[brother hero July training] day 25: tree array
Leetcode 0137. number II that appears only once
链表-合并两个排序的列表
8.2 一些代数知识(群、循环群和子群)
J9 number theory: how to avoid the trap of stepping on thunder?
Hello World
Linked list - reverse linked list
Netease game R & D Engineer Intern (client side)
SSH based online mall
如何做好测试用例设计
The third day of SSM practice_ Paging assistant_ Security framework
Linked list - the penultimate K nodes
数据安全知识体系
链表-倒数最后k个结点
神经网络学习(2)前言介绍二