当前位置:网站首页>七牛云上传图片和本地上传
七牛云上传图片和本地上传
2022-08-04 06:07:00 【晨馨^@^】
首先是本地上传
思路:
1.创建视图函数
2.采用post创建的方法
3.获取模型类中的img图片数据
4.获取图片的后缀名
5.将将链接进行拼接
6.对数据进行保存和上传
导包
import jwt
from flask import Blueprint, jsonify, json, current_app, request, g
from flask_restful import Api,Resource,reqparse
from qiniu import Auth, put_file
from ronglian_sms_sdk import SmsSDK
import random,redis
from model.models import db
from model.models import Eser,Aser
import time
代码实现****
# 1.创建视图函数 我的函数命名为ImgView
class ImgView(Resource):
def post(self):# 2.采用post方法进行创建
img=request.file.get('img') # 3.获取模型类中的数据库数据
png=img.filename.spilt('.')[-1] # 4.获取图片的后缀
static_path=f'创建的文件名(必须提前创好不然会报错)/{
str(int(time.time()))+str(random.randint(100000,999999))}.{
png}' # 5.拼接图片的连接
img.save(static_path) # 保存
上传至七牛云
思路
1.基于本地上传进行下一步操作
2.获取七牛云ak
3.获取七牛云的sk然后进行相关的操作
相关代码
# 1.创建视图函数 我的函数命名为ImgView
class ImgView(Resource):
def post(self):# 2.采用post方法进行创建
ak=''
sk=''
q=Auth(ak,sk)
img=request.file.get('img') # 3.获取模型类中的数据库数据
png=img.filename.spilt('.')[-1] # 4.获取图片的后缀
static_path=f'创建的文件名(必须提前创好不然会报错)/{
str(int(time.time()))+str(random.randint(100000,999999))}.{
png}' # 5.拼接图片的连接
img.save(static_path) # 保存
token=q.upload_token('空间名',static_path)
ret=put_file(token,static_path,static_path)
if ret[0]['key']!=static_path:
return jsonify({
'code':400,
'msg':'上传失败',
})
db.session.add(Aser( # 对将图片存到文件夹里
img=static_path
))
db.session.commit() # 进行提交
return jsonify({
'code':200,
'msg':'上传成功',
"data":static_path
})
边栏推荐
- 数组的一些方法
- 代码小变化带来的大不同
- unicloud 腾讯云 上传文件 Have no access right to the storage uniapp
- Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!
- 如何用matlab做高精度计算?【第三辑】(完)
- Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
- 七夕送礼,心愿直抵!
- 科研绘图图表类型种类繁多,本文告诉你如何选择!
- FCN - the originator of semantic segmentation (based on tf-Kersa reproduction code)
- SQL去重的三种方法汇总
猜你喜欢
Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!
idea使用@Autowired注解爆红原因及解决方法
手把手教你Charles抓包工具使用
Nacos 原理
app逆向1某联
Based on the EEMD + + MLR GRU helped time series prediction
窥探晶体世界的奥秘 —— 230种空间群晶体结构模型全在这里
【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
Microsoft computer butler 2.0 beta experience
拒绝碰运气,导师人品这样了解!
随机推荐
Unable to preventDefault inside passive event listener due to target being treated as passive. See
SystemVerilog-条件(三元)运算符
软件稳定性思考
JVM调优实践
likeshop外卖点餐系统开源啦100%开源无加密
What is the connection between GRNN, RBF, PNN, KELM?
this关键字,构造函数
代码小变化带来的大不同
Database document generation tool V1.0
有人试过用NPGsql驱动连接openGauss开发应用的吗?
七夕送礼,心愿直抵!
反序列化字符逃逸漏洞之
原型图总结规范
Database knowledge: SQLServer creates non-sa user notes
对象的扩展补充
SQL去重的三种方法汇总
Provide 和 Inject 的用法
npm包发布与迭代
mysql月份比較是否相等
JVM 快速检测死锁