当前位置:网站首页>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
}
})
边栏推荐
- Alibaba code index technology practice: provide reading experience of local IDE for code review
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- Oracle super full SQL, details crazy
- MySQL的存储过程
- Using recursion and chain header interpolation to realize the group turnover of linked lists -- leetcode25 K group turnover linked lists
- vulnhub:SolidState
- Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接
- CV semantic segmentation model sketch (2)
- [ESN] learning echo state network
- With this, your messages can't be monitored
猜你喜欢

Still writing a lot of if to judge? A rule executor kills all if judgments in the project

17. Design of machine learning system

Idea connection database

CV instance segmentation model sketch (1)

Dynamic programming problem (3)

Geth installation
![[CNN] Why is the convolution kernel size of CNN usually odd](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[CNN] Why is the convolution kernel size of CNN usually odd

Linux下安装Mysql5.7,超详细完整教程,以及云mysql连接

IDEA 连接 数据库

vscode下链接远程服务器安装插件失败、速度慢等解决方法
随机推荐
Idea2021.2 installation and configuration (continuous update)
Feign call fails. JSON parse error illegal character ((ctrl-char, code 31)) only regular white space (R
动态规划问题(二)
[development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver
CV semantic segmentation model sketch (2)
MySql中的like和in走不走索引
16.偏差、方差、正则化、学习曲线对模型的影响
聊聊异步编程的 7 种实现方式
How to learn R language
Use hutool tool class to operate excel with more empty Sheet1
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
Introduction and solution of common security vulnerabilities in web system CSRF attack
动态规划问题(三)
Compilation principle research study topic 2 -- recursive descent syntax analysis design principle and Implementation
Redis learning notes
Attack and defense world web master advanced area PHP_ rce
Recursion / backtracking (middle)
Event extraction and documentation (2018)
MySQL stored procedure
PTA (daily question) 7-71 character trapezoid