Python的兩種上傳圖片方式
- 上傳至七牛云服務器
- 上傳至自己服務器
上傳至七牛云服務器
代碼注釋寫的已經很清楚了,直接可以用
access_key = '替換成你的'
# 個人中心->密匙管理->SK
secret_key = '替換成你的'
# 七牛空間名
bucket_name = '替換成你的'
#臨時域名
url = '替換成你的'
q = qiniu.Auth(access_key, secret_key)
def qiniu_upload(key, localfile):
token = q.upload_token(bucket_name, key, 3600)
ret, info = qiniu.put_file(token, key, localfile)
if ret:
return 'http://{0}/{1}'.format(url, ret['key'])
else:
raise Exception('上傳失敗,請重試')
@csrf_exempt
def upload_qiniu(request):
"""
@api {POST} /upload_qiniu/ [上傳圖片至七牛]
* @apiVersion 0.0.1
* @apiGroup upload
@apiParamExample {params} 請求參數
"image":"" "圖片文件"
@apiSuccessExample {json} 成功返回
{
"message": "",
"next": "",
"data": "",
"response": "ok",
"error": ""
}
@apiSuccessExample {json} 失敗返回
{
"message": "",
"next": "",
"data": null,
"response": "fail",
"error": "上傳失敗","缺少參數"
}
"""
image = request.FILES.get("image")
if not image:
return ajax.jsonp_fail(request, u"缺少參數")
service_name = save_block_file(image)
data=qiniu_upload(service_name,get_absolute_file_path(service_name))
if data:
return ajax.jsonp_ok(request, data)
else:
return ajax.jsonp_fail(request, u"上傳失敗")
def save_upload_file(new_file_path, raw_file,name):
"""
功能說明:保存上傳文件
raw_file:原始文件對象
new_file_path:新文件絕對路徑
"""
try:
# 如果新文件存在則刪除
if os.path.exists(new_file_path):
try:
os.remove(new_file_path)
except:
pass
content = raw_file.read()
fp = open(new_file_path, 'wb')
fp.write(content)
fp.close()
return name
except Exception as e:
print (e)
return False
def save_block_file(block_file):
"""
:param block_file: 文件對象
:return:
"""
# 唯一標識 + 文件名 201801171.png
now_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
name = '%s%s' % (now_time, block_file.name)
block_file_path = get_absolute_file_path(name).replace("\\", "/")
# 文件上傳保存
return save_upload_file(block_file_path, block_file, name)
def get_absolute_file_path(file_name):
"""
功能說明:返回絕對路徑字符串
file_name:文件名字
"""
media_root = settings.UPLOAD
print ("media_root",media_root)
absolute_file_path = os.path.join(media_root, file_name)
print("absolute_file_path", absolute_file_path)
# 返回文件絕對路徑中目錄路徑
file_dir = os.path.dirname(absolute_file_path)
print ("file_dir", file_dir)
if not os.path.exists(file_dir):
# 創建路徑
os.makedirs(file_dir)
return absolute_file_path
上傳至自己服務器
@csrf_exempt
def insert_img(request):
"""
@api {POST} /upload/ [上傳圖片]
* @apiVersion 0.0.1
* @apiGroup note
@apiParamExample {params} 請求參數
"image":"" "圖片文件"
"""
image = request.FILES.get("image")
if not image:
return ajax.jsonp_fail(request, u"缺少參數")
service_name = save_block_file(image)
path = '%s/%s' % ("你的服務器地址", service_name)
if not path:
return ajax.jsonp_fail(request, u"上傳失敗")
else:
return ajax.jsonp_ok(request, path)
def save_upload_file(new_file_path, raw_file,name):
"""
功能說明:保存上傳文件
raw_file:原始文件對象
new_file_path:新文件絕對路徑
"""
try:
# 如果新文件存在則刪除
if os.path.exists(new_file_path):
try:
os.remove(new_file_path)
except:
pass
content = raw_file.read()
fp = open(new_file_path, 'wb')
fp.write(content)
fp.close()
return name
except Exception as e:
print(e)
return False
def save_block_file(block_file):
"""
:param block_file: 文件對象
:return:
"""
# 唯一標識 + 文件名 201801171.png
now_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
name = '%s%s' % (now_time, block_file.name)
block_file_path = get_absolute_file_path(name).replace("\\", "/")
# 文件上傳保存
return save_upload_file(block_file_path, block_file,name)
def get_absolute_file_path(file_name):
"""
功能說明:返回絕對路徑字符串
file_name:文件名字
"""
media_root = settings.UPLOAD
print("media_root",media_root)
absolute_file_path = os.path.join(media_root, file_name)
print("absolute_file_path", absolute_file_path)
# 返回文件絕對路徑中目錄路徑
file_dir = os.path.dirname(absolute_file_path)
print("file_dir", file_dir)
if not os.path.exists(file_dir):
# 創建路徑
os.makedirs(file_dir)
return absolute_file_path
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
