Skip to content

Commit da37d1a

Browse files
authored
Merge pull request #284 from l-iberty/master
update demo
2 parents 5536f8c + 79e909b commit da37d1a

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

demo/get_presigned_url.py

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import logging
77
import requests
8+
import time
89

910
# 正常情况日志级别使用 INFO,需要定位时可以修改为 DEBUG,此时 SDK 会打印和服务端的通信信息
1011
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
@@ -76,8 +77,16 @@
7677
print(url)
7778

7879
# 使用上传 URL
79-
response = requests.put(url=url, data=b'123')
80-
print(response)
80+
retry = 1 # 简单重试1次
81+
for i in range(retry + 1):
82+
response = requests.put(url=url, data=b'123')
83+
if response.status_code == 400 or response.status_code >= 500: # 只对400和5xx错误码进行重试
84+
time.sleep(1) # 延迟 1s 后再重试
85+
continue
86+
# 请求结束, 打印结果并退出循环
87+
print(response)
88+
break
89+
8190

8291
'''生成下载预签名 URL
8392
'''
@@ -136,8 +145,16 @@
136145
print(url)
137146

138147
# 使用下载URL
139-
response = requests.get(url)
140-
print(response)
148+
retry = 1 # 简单重试1次
149+
for i in range(retry + 1):
150+
response = requests.get(url)
151+
if response.status_code == 400 or response.status_code >= 500: # 只对400和5xx错误码进行重试
152+
time.sleep(1) # 延迟 1s 后再重试
153+
continue
154+
# 请求结束, 打印结果并退出循环
155+
print(response)
156+
break
157+
141158

142159
'''使用临时密钥生成下载预签名 URL
143160
'''
@@ -157,8 +174,16 @@
157174
print(url)
158175

159176
# 使用下载 URL
160-
response = requests.get(url)
161-
print(response)
177+
retry = 1 # 简单重试1次
178+
for i in range(retry + 1):
179+
response = requests.get(url)
180+
if response.status_code == 400 or response.status_code >= 500: # 只对400和5xx错误码进行重试
181+
time.sleep(1) # 延迟 1s 后再重试
182+
continue
183+
# 请求结束, 打印结果并退出循环
184+
print(response)
185+
break
186+
162187

163188
'''生成下载对象的预签名 URL
164189
'''
@@ -222,5 +247,12 @@
222247
print(url)
223248

224249
# 使用下载 URL
225-
response = requests.get(url)
226-
print(response)
250+
retry = 1 # 简单重试1次
251+
for i in range(retry + 1):
252+
response = requests.get(url)
253+
if response.status_code == 400 or response.status_code >= 500: # 只对400和5xx错误码进行重试
254+
time.sleep(1) # 延迟 1s 后再重试
255+
continue
256+
# 请求结束, 打印结果并退出循环
257+
print(response)
258+
break

0 commit comments

Comments
 (0)