当前位置:网站首页>flask与七牛云上传图片
flask与七牛云上传图片
2022-07-28 22:38:00 【Wbig】
1. 认识七牛云
七牛云相当是一个网上存储空间,可以直接在浏览器里访问存储的图片。
根据CDN分发给不同的网络节点,浏览器就近取材,拿到图片,渲染页面
当然,在七牛云存储的图片也需要存储到本地一份。
2. 安装七牛云
在终端输入
pip install qiniu
安装即可
3. 使用七牛云
为七牛云创建一个独有的文件,去使用它,对于自己在七牛云官网上拿到的ak,sk等信息,要进行封装加密处理
from flask import current_app
from qiniu import Auth,put_file
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_NAME")
def upload(self,loacfilepath,newfilename):
token = self.q.upload_token(self.bucket_name,newfilename,3600)
print(token)
res = put_file(token,newfilename,loacfilepath,version="v2")
print(res)
if res[0]["key"] == newfilename:
return newfilename
else:
return False
if __name__ == '__main__':
from app import app
with app.app_context():
QiniuTool().upload("./../static/exo.jpg","static/img/exo.jpg")
封站内容,调用时直接根据自己的文件路径导入
class SecretConfig():
QINIU_AK = "Ae95CnxjK5lryTj-Fb1Z_4sLWba_7XShIrvZBQoN"
QINIU_SK = "j5smR9D8hrf5DC2PnXD1XamwYWLyUJ1OdvaOPH9m"
QINIU_BUCKET_NAME = "p7uploadimg"
QINIU_URL = "http://rfluoehnd.hn-bkt.clouddn.com/"
4.flask与七牛云上传图片
上传图片需要先上传到本地在根据本地上传到七牛云
class UploadView(Resource):
@login
def post(self):
img = request.files.get("img")
ext = img.filename.split(".")[-1]
now_time = datetime.strftime(datetime.now(),"%Y%m%d%H%M%S")
r = str(random.randint(100000,999999))
new_name = "%s%s.%s"%(now_time,r,ext)
save_path = "static/%s"%new_name
print(new_name)
img.save(save_path)
res = QiniuTool().upload(save_path,save_path)
if not res:
return jsonify({
"code": 400,
"msg": "文件上传失败"
})
user_info = UserModel.query.get(g.uid)
user_info.img = save_path
db.session.commit()
return jsonify({
"code": 200,
"msg": "上传成功",
"data": {
"key":res
}
})
边栏推荐
- Advanced area of attack and defense world web masters ics-06
- Applet verification code login
- 110 MySQL interview questions and answers (continuously updated)
- Talk about seven ways to realize asynchronous programming
- 动态规划问题(六)
- 乱打日志的男孩运气怎么样我不知道,加班肯定很多!
- SAP VL02N 交货单过账函数 WS_DELIVERY_UPDATE
- Idea error running 'application' command line is too long solution
- @Transactional 注解使用详解
- Oracle实例无法启动的问题如何解决
猜你喜欢

Web系统常见安全漏洞介绍及解决方案-sql注入

vscode下链接远程服务器安装插件失败、速度慢等解决方法

MySql中的like和in走不走索引

聊聊异步编程的 7 种实现方式

IDEA2021.2安装与配置(持续更新)
![[applet project development -- JD mall] uni app commodity classification page (first)](/img/6c/5b92fc1f18d58e0fdf6f1896188fcd.png)
[applet project development -- JD mall] uni app commodity classification page (first)

Recursion / backtracking (Part 2)

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

动态规划问题(八)

Alibaba code index technology practice: provide reading experience of local IDE for code review
随机推荐
Application and principle of distributed current limiting redistribution rratelimiter
ES6 operation tutorial
[micro services ~nacos] Nacos service providers and service consumers
2022 network security learning route is very detailed, recommended Learning
R语言怎么学
Advanced area of attack and defense world web masters -baby Web
Concurrency in go
【飞控开发基础教程8】疯壳·开源编队无人机-I2C(激光测距)
乱打日志的男孩运气怎么样我不知道,加班肯定很多!
PTA (daily question) 7-77 encryption
Multimodal model sketch (1)
Advanced area of attack and defense world web masters warmup
CV instance segmentation model sketch (1)
Event extraction and documentation (2008-2017)
Alibaba code index technology practice: provide reading experience of local IDE for code review
最长上升子序列
Visual full link log tracking
PHP语言基础知识(超详细)
Summary: the difference between pod and container
Where is sandbox's confidence in rejecting meta's acquisition of meta universe leader sand?