Skip to content

Commit edf1714

Browse files
committed
add backup host
1 parent 7f49b76 commit edf1714

File tree

4 files changed

+35
-17
lines changed

4 files changed

+35
-17
lines changed

qiniu/io.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def put(uptoken, key, data, extra=None):
5353
{'filename': fname, 'data': data, 'mime_type': extra.mime_type},
5454
]
5555
ret, err, code = rpc.Client(conf.UP_HOST).call_with_multipart("/", fields, files)
56+
print err
5657
if err is None or code == 571 or code == 614:
5758
return ret, err
5859

qiniu/resumable_io.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,25 @@ def put_file(uptoken, key, localfile, extra):
6666
""" 上传文件 """
6767
f = open(localfile, "rb")
6868
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)
7070
f.close()
71-
return ret
71+
return ret, err
7272

73-
74-
def put(uptoken, key, f, fsize, extra, host=None):
73+
def put(uptoken, key, f, fsize, extra):
7574
""" 上传二进制流, 通过将data "切片" 分段上传 """
7675
if not isinstance(extra, PutExtra):
7776
print("extra must the instance of PutExtra")
7877
return
78+
host = conf.UP_HOST
79+
ret, err, code = put_with_host(uptoken, key, f, fsize, extra, host)
80+
if err is None or code == 571 or code == 614:
81+
return ret, err
82+
83+
ret, err, code = put_with_host(uptoken, key, f, fsize, extra, conf.UP_HOST2)
84+
return ret, err
7985

80-
if host is None:
81-
host = conf.UP_HOST
8286

87+
def put_with_host(uptoken, key, f, fsize, extra, host):
8388
block_cnt = block_count(fsize)
8489
if extra.progresses is None:
8590
extra.progresses = [None] * block_cnt
@@ -113,15 +118,14 @@ def put(uptoken, key, f, fsize, extra, host=None):
113118
mkfile_client = auth_up.Client(uptoken, mkfile_host)
114119
return mkfile(mkfile_client, key, fsize, extra, mkfile_host)
115120

116-
117121
def resumable_block_put(block, index, extra, uptoken, host):
118122
block_size = len(block)
119123

120124
mkblk_client = auth_up.Client(uptoken, host)
121125
if extra.progresses[index] is None or "ctx" not in extra.progresses[index]:
122126
crc32 = gen_crc32(block)
123127
block = bytearray(block)
124-
extra.progresses[index], err = mkblock(mkblk_client, block_size, block, host)
128+
extra.progresses[index], err, code = mkblock(mkblk_client, block_size, block, host)
125129
if err is not None:
126130
extra.notify_err(index, block_size, err)
127131
return err

qiniu/test/io_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_put_StringIO():
114114

115115
def test_put_urlopen():
116116
key = "test_%s" % r(9)
117-
data = urllib.urlopen('http://cheneya.qiniudn.com/hello_jpg')
117+
data = urllib.urlopen('http://pythonsdk.qiniudn.com/hello.jpg')
118118
ret, err = io.put(policy.token(), key, data)
119119
assert err is None
120120
assert ret['key'] == key
@@ -155,6 +155,7 @@ def read(self, n=None):
155155
test_put_urlopen()
156156
test_put_no_length()
157157

158+
158159
def test_put_file(self):
159160
localfile = "%s" % __file__
160161
key = "test_%s" % r(9)
@@ -178,6 +179,18 @@ def test_put_fail_reqid(self):
178179
ret, err = io.put("", key, data, extra)
179180
assert "reqid" in err
180181

182+
def test_put_with_uphost2(self):
183+
#mistake up host
184+
conf.UP_HOST = "api.qiniu.com"
185+
localfile = "%s" % __file__
186+
key = "test_up2_%s" % r(9)
187+
188+
extra.check_crc = 1
189+
ret, err = io.put_file(policy.token(), key, localfile, extra)
190+
assert err is None
191+
assert ret['key'] == key
192+
conf.UP_HOST = "up.qiniu.com"
193+
181194

182195
class Test_get_file_crc32(unittest.TestCase):
183196

qiniu/test/resumable_io_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ class TestBlock(unittest.TestCase):
3737
def test_block(self):
3838
if is_travis:
3939
return
40+
host = conf.UP_HOST
4041
policy = rs.PutPolicy(bucket)
4142
uptoken = policy.token()
4243
client = up.Client(uptoken)
4344

4445
# rets = [0, 0]
4546
data_slice_2 = "\nbye!"
46-
ret, err = resumable_io.mkblock(
47-
client, len(data_slice_2), data_slice_2)
47+
ret, err, code = resumable_io.mkblock(
48+
client, len(data_slice_2), data_slice_2, host)
4849
assert err is None, err
4950
self.assertEqual(ret["crc32"], binascii.crc32(data_slice_2))
5051

@@ -56,16 +57,16 @@ def test_block(self):
5657
lens += extra.progresses[i]["offset"]
5758

5859
key = u"sdk_py_resumable_block_4_%s" % r(9)
59-
ret, err = resumable_io.mkfile(client, key, lens, extra)
60-
assert err is None, err
60+
ret, err, code = resumable_io.mkfile(client, key, lens, extra, host)
61+
assert err is None
6162
self.assertEqual(
6263
ret["hash"], "FtCFo0mQugW98uaPYgr54Vb1QsO0", "hash not match")
6364
rs.Client().delete(bucket, key)
6465

6566
def test_put(self):
6667
if is_travis:
6768
return
68-
src = urllib.urlopen("http://cheneya.qiniudn.com/hello_jpg")
69+
src = urllib.urlopen("http://pythonsdk.qiniudn.com/hello.jpg")
6970
ostype = platform.system()
7071
if ostype.lower().find("windows") != -1:
7172
tmpf = "".join([os.getcwd(), mktemp()])
@@ -84,8 +85,7 @@ def test_put(self):
8485
ret, err = resumable_io.put_file(policy.token(), key, localfile, extra)
8586
dst.close()
8687
os.remove(tmpf)
87-
88-
assert err is None, err
88+
assert err is None
8989
assert ret.get("x:foo") == "test", "return data not contains 'x:foo'"
9090
self.assertEqual(
9191
ret["hash"], "FnyTMUqPNRTdk1Wou7oLqDHkBm_p", "hash not match")
@@ -112,7 +112,7 @@ def test_put_4m(self):
112112
ret, err = resumable_io.put_file(policy.token(), key, localfile, extra)
113113
dst.close()
114114
os.remove(tmpf)
115-
115+
print err
116116
assert err is None, err
117117
assert ret.get("x:foo") == "test", "return data not contains 'x:foo'"
118118
self.assertEqual(

0 commit comments

Comments
 (0)