Flask添加七牛雲上傳文件圖片系統python
七牛雲python sdk:https://developer.qiniu.com/kodo/sdk/1242/pythonweb
在項目中添加七牛雲圖片上傳系統django
首先你須要註冊一個七牛雲圖片上傳系統的帳號,固然他們獲取的我的信息至關多,若是不喜歡,能夠用fdfs,可是我沒找到fdfs在flask中配置的教程,不過也就是django中的遷移裝飾器改一下,而後設置中改一下就好了。(固然也就是想一想,其實那個遷移裝飾器就寫不來),因此既然沒有辦法裝逼用fdfs,那就只能用七牛雲來作了
直接上代碼
這個放在我本身的utils文件夾中flask
from qiniu import Auth,put_file,put_data,put_stream
access_key='七牛雲給你的ak'api
secret_key='七牛雲給你的sk'函數
def upload_file_qiniu(input_data):
#create permission object
建立認證對象
q=Auth(access_key=access_key,secret_key=secret_key)
你的儲存庫裏的倉庫名字
bucket_name='cars'
# generate upload token
自動生成一個上傳token
token=q.upload_token(bucket=bucket_name)
#upload file
上傳文件,返回值中會有圖片路徑信息和狀態碼
ret1,ret2=put_data(token,None,data=input_data)
print('this is result')
print(ret1)
print(ret2)
#judge file upload is success or not
用返回數據判斷上傳是否成功
if ret2.status_code !=200:
raise Exception('file upload failure')post
返回圖片路徑信息
return ret1.get('key')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
建立模塊ui
在咱們以前的api文件夾下建立一個新模塊,模塊名任意,在模塊藍圖中註冊一下藍圖this
from flask import Blueprint
# create blueprint
api=Blueprint('api_1_0',__name__)url
from . import register,verify,cars
1
2
3
4
5
而後再在模塊中導入api,而後再寫一個藍圖路徑裝飾器
from . import api
容許裝飾器內的視圖函數使用post方法
@api.route('/upload_image',methods=['post'])
1
2
3
配置常量表
在以前建立的constants裏面加上這些東西,主要是自定義狀態碼和七牛雲圖片查找路徑前綴
IMAGE_UUID_REDIS_EXPIRES=60
PHONE_MSG_CODE_EXPIRES=2*60
PHONE_FLAG_CODE_EXPIRES=2*60
string_test_phonenum=r'^1[1234567890]{10}$'
string_test_username=r'^[\w]{6,18}$'
string_test_email='^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$'
RETURN_OKAY=0 #no error report
RETURN_LOGin=4001 #not login
RETURN_IMAGE_INPUT_ERR=4002 #front website data aquired error
RETURN_IMAGE_UPLOAD_ERR=4003 #front website data aquired error
qiniu_url='http://puek1pi1o.bkt.clouddn.com/'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
---------------------