当前位置:网站首页>Flash and seven cattle cloud upload pictures
Flash and seven cattle cloud upload pictures
2022-07-29 00:36:00 【Wbig】
The use of seven cattle cloud
1. Know Qi niuyun
Qiniu cloud is quite an online storage space , You can access the stored pictures directly in the browser .
according to CDN Distribute to different network nodes , The browser fetches materials nearby , Get the picture , Render the page
Of course , Pictures stored in qiniu cloud also need to be stored locally .
2. Installation of seven cattle cloud
Input... At the terminal
pip install qiniu
Can be installed
3. Use qiniu cloud
Create a unique file for qiniu cloud , To use it , For what I got on the official website of qiniuyun ak,sk Etc , To perform encapsulation and encryption
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")
Content of station closure , When calling, import directly according to your own file path
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 Upload pictures with seven cattle cloud
To upload pictures, you need to upload them to the local first, and then upload them to qiniu cloud according to the local
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": " File upload failed "
})
user_info = UserModel.query.get(g.uid)
user_info.img = save_path
db.session.commit()
return jsonify({
"code": 200,
"msg": " Upload successful ",
"data": {
"key":res
}
})
边栏推荐
- flask结合容联云发送验证码
- MySQL transaction (this is enough...)
- Longest ascending subsequence
- 乱打日志的男孩运气怎么样我不知道,加班肯定很多!
- Still writing a lot of if to judge? A rule executor kills all if judgments in the project
- Soft test --- database (4) SQL statement
- Dynamic programming (V)
- 12个MySQL慢查询的原因分析
- Alibaba code index technology practice: provide reading experience of local IDE for code review
- Locally connect to redis on Windows Server
猜你喜欢
【飞控开发基础教程8】疯壳·开源编队无人机-I2C(激光测距)
Dynamic programming problem (3)
Geth installation
flyway的快速入门教程
分布式限流 redission RRateLimiter 的使用及原理
How to solve the problems of MQ message loss, duplication and backlog?
Locally connect to redis on Windows Server
递归/回溯刷题(下)
还在写大量 if 来判断?一个规则执行器干掉项目中所有的 if 判断...
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
随机推荐
Camera Hal OEM模块 ---- cmr_preview.c
Advanced area of attack and defense world web masters unserialize3
[develop low code platform] low code rendering
Basic knowledge of PHP language (super detailed)
Advanced area of attack and defense world web masters warmup
动态规划问题(七)
PTA (one question per day) 7-76 ratio
flask结合容联云发送验证码
pnpm的安装与使用
动态规划问题(三)
vscode下链接远程服务器安装插件失败、速度慢等解决方法
Newscenter, advanced area of attack and defense world web masters
Shell编程规范与变量
Dynamic programming problem (2)
Samsung asset management (Hong Kong) launched yuancosmos ETF to focus on investing in the future tuyere track
html+css+php+mysql实现注册+登录+修改密码(附完整代码)
Immutable x officially opens IMX token pledge detailed IMX pledge introduction optimistic about the development prospect of IMX
CDN mode uses vant components, and components cannot be called after they are introduced
MySQL sub database and sub table and its smooth expansion scheme
2022dasctfjuly empowerment competition (reappearance)