-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
124 lines (111 loc) · 3.86 KB
/
test.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/4 9:18
# @Author : Weiqiang.long
# @Site :
# @File : test.py
# @Software: PyCharm
import json
import oss2
from flask import Flask, render_template, request, Response
app = Flask(__name__)
@app.route("/getossurl", methods=['POST', 'GET'])
def GetOss():
"""
生成签名URL
:param path:文件名
:param time:授权有效期,默认授权有效期为四天
:return:签名URL
"""
MyAccessKeyId = 'XXX'
MyAccessKeySecret = 'XXX'
MyEndpoint = 'XXX'
MyBucketName = 'XXX'
if request.method == "POST":
path = request.form.get('path')
times = request.form.get('times')
# 为path缺失组装一个json
path_error = {
"code": 500,
"msg": "path参数缺失!"
}
if path == None:
return Response(json.dumps(path_error), mimetype='application/json')
elif times == None:
times = 4
# 换算time,用天数乘以86400秒(一天)
time = times * 86400
# print(times)
auth = oss2.Auth(MyAccessKeyId, MyAccessKeySecret)
bucket = oss2.Bucket(auth, MyEndpoint, MyBucketName)
oss_url = bucket.sign_url('GET', path, time)
code = '00'
msg = u'请求成功'
dict_data = {
"code": code,
"msg": msg,
"url": oss_url
}
return Response(json.dumps(dict_data), mimetype='application/json')
else:
# 转换times的数据类型为int
times = int(times)
# 换算time,用天数乘以86400秒(一天)
time = times * 86400
# print(times)
auth = oss2.Auth(MyAccessKeyId, MyAccessKeySecret)
bucket = oss2.Bucket(auth, MyEndpoint, MyBucketName)
oss_url = bucket.sign_url('GET', path, time)
code = '00'
msg = u'请求成功'
dict_data = {
"code": code,
"msg": msg,
"url": oss_url
}
return Response(json.dumps(dict_data), mimetype='application/json')
if request.method == "GET":
path = request.args.get('path')
times = request.args.get('times')
# 为path缺失组装一个json
path_error = {
"code": 500,
"msg": "path参数缺失!"
}
if path == None:
return Response(json.dumps(path_error), mimetype='application/json')
elif times == None:
times = 4
# 换算time,用天数乘以86400秒(一天)
time = times * 86400
# print(times)
auth = oss2.Auth(MyAccessKeyId, MyAccessKeySecret)
bucket = oss2.Bucket(auth, MyEndpoint, MyBucketName)
oss_url = bucket.sign_url('GET', path, time)
code = '00'
msg = u'请求成功'
dict_data = {
"code": code,
"msg": msg,
"url": oss_url
}
return Response(json.dumps(dict_data), mimetype='application/json')
else:
# 转换times的数据类型为int
times = int(times)
# 换算time,用天数乘以86400秒(一天)
time = times * 86400
# print(times)
auth = oss2.Auth(MyAccessKeyId, MyAccessKeySecret)
bucket = oss2.Bucket(auth, MyEndpoint, MyBucketName)
oss_url = bucket.sign_url('GET', path, time)
code = '00'
msg = u'请求成功'
dict_data = {
"code": code,
"msg": msg,
"url": oss_url
}
return Response(json.dumps(dict_data), mimetype='application/json')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)