@@ -66,23 +66,35 @@ def put_file(uptoken, key, localfile, extra):
66
66
""" 上传文件 """
67
67
f = open (localfile , "rb" )
68
68
statinfo = os .stat (localfile )
69
- ret = put (uptoken , key , f , statinfo .st_size , extra )
69
+ ret , err = put (uptoken , key , f , statinfo .st_size , extra )
70
70
f .close ()
71
- return ret
71
+ return ret , err
72
72
73
73
74
74
def put (uptoken , key , f , fsize , extra ):
75
75
""" 上传二进制流, 通过将data "切片" 分段上传 """
76
76
if not isinstance (extra , PutExtra ):
77
77
print ("extra must the instance of PutExtra" )
78
78
return
79
+ host = conf .UP_HOST
80
+ try :
81
+ ret , err , code = put_with_host (uptoken , key , f , fsize , extra , host )
82
+ if err is None or code == 571 or code == 614 or code == 301 :
83
+ return ret , err
84
+ except :
85
+ pass
79
86
87
+ ret , err , code = put_with_host (uptoken , key , f , fsize , extra , conf .UP_HOST2 )
88
+ return ret , err
89
+
90
+
91
+ def put_with_host (uptoken , key , f , fsize , extra , host ):
80
92
block_cnt = block_count (fsize )
81
93
if extra .progresses is None :
82
94
extra .progresses = [None ] * block_cnt
83
95
else :
84
96
if not len (extra .progresses ) == block_cnt :
85
- return None , err_invalid_put_progress
97
+ return None , err_invalid_put_progress , 0
86
98
87
99
if extra .try_times is None :
88
100
extra .try_times = _try_times
@@ -97,27 +109,29 @@ def put(uptoken, key, f, fsize, extra):
97
109
read_length = fsize - i * _block_size
98
110
data_slice = f .read (read_length )
99
111
while True :
100
- err = resumable_block_put (data_slice , i , extra , uptoken )
112
+ err = resumable_block_put (data_slice , i , extra , uptoken , host )
101
113
if err is None :
102
114
break
103
115
104
116
try_time -= 1
105
117
if try_time <= 0 :
106
- return None , err_put_failed
118
+ return None , err_put_failed , 0
107
119
print err , ".. retry"
108
120
109
- mkfile_client = auth_up .Client (uptoken , extra .progresses [- 1 ]["host" ])
110
- return mkfile (mkfile_client , key , fsize , extra )
121
+ mkfile_host = extra .progresses [- 1 ]["host" ] if block_cnt else host
122
+ mkfile_client = auth_up .Client (uptoken , mkfile_host )
123
+
124
+ return mkfile (mkfile_client , key , fsize , extra , host )
111
125
112
126
113
- def resumable_block_put (block , index , extra , uptoken ):
127
+ def resumable_block_put (block , index , extra , uptoken , host ):
114
128
block_size = len (block )
115
129
116
- mkblk_client = auth_up .Client (uptoken , conf . UP_HOST )
130
+ mkblk_client = auth_up .Client (uptoken , host )
117
131
if extra .progresses [index ] is None or "ctx" not in extra .progresses [index ]:
118
132
crc32 = gen_crc32 (block )
119
133
block = bytearray (block )
120
- extra .progresses [index ], err = mkblock (mkblk_client , block_size , block )
134
+ extra .progresses [index ], err , code = mkblock (mkblk_client , block_size , block , host )
121
135
if err is not None :
122
136
extra .notify_err (index , block_size , err )
123
137
return err
@@ -132,8 +146,8 @@ def block_count(size):
132
146
return (size + _block_mask ) / _block_size
133
147
134
148
135
- def mkblock (client , block_size , first_chunk ):
136
- url = "http://%s/mkblk/%s" % (conf . UP_HOST , block_size )
149
+ def mkblock (client , block_size , first_chunk , host ):
150
+ url = "http://%s/mkblk/%s" % (host , block_size )
137
151
content_type = "application/octet-stream"
138
152
return client .call_with (url , first_chunk , content_type , len (first_chunk ))
139
153
@@ -145,8 +159,8 @@ def putblock(client, block_ret, chunk):
145
159
return client .call_with (url , chunk , content_type , len (chunk ))
146
160
147
161
148
- def mkfile (client , key , fsize , extra ):
149
- url = ["http://%s/mkfile/%s" % (conf . UP_HOST , fsize )]
162
+ def mkfile (client , key , fsize , extra , host ):
163
+ url = ["http://%s/mkfile/%s" % (host , fsize )]
150
164
151
165
if extra .mimetype :
152
166
url .append ("mimeType/%s" % urlsafe_b64encode (extra .mimetype ))
0 commit comments