Skip to content

Commit cb0d415

Browse files
authored
fix batch rs host (#445)
1 parent 4d54f21 commit cb0d415

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

qiniu/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
from .services.compute.qcos_api import QcosClient
2929
from .services.sms.sms import Sms
3030
from .services.pili.rtc_server_manager import RtcServer, get_room_token
31-
from .utils import urlsafe_base64_encode, urlsafe_base64_decode, etag, entry, canonical_mime_header_key
31+
from .utils import urlsafe_base64_encode, urlsafe_base64_decode, etag, entry, decode_entry, canonical_mime_header_key

qiniu/services/storage/bucket.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from qiniu import config, QiniuMacAuth
44
from qiniu import http
5-
from qiniu.utils import urlsafe_base64_encode, entry
5+
from qiniu.utils import urlsafe_base64_encode, entry, decode_entry
66

77

88
class BucketManager(object):
@@ -344,7 +344,20 @@ def batch(self, operations):
344344
]
345345
一个ResponseInfo对象
346346
"""
347-
url = '{0}/batch'.format(config.get_default('default_rs_host'))
347+
if not operations:
348+
raise Exception('operations is empty')
349+
bucket = ''
350+
for op in operations:
351+
segments = op.split('/')
352+
e = segments[1] if len(segments) >= 2 else ''
353+
bucket, _ = decode_entry(e)
354+
if bucket:
355+
break
356+
if not bucket:
357+
raise Exception('bucket is empty')
358+
ak = self.auth.get_access_key()
359+
rs_host = self.zone.get_rs_host(ak, bucket)
360+
url = '{0}/batch'.format(rs_host)
348361
return self.__post(url, dict(op=operations))
349362

350363
def buckets(self):

qiniu/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ def entry(bucket, key):
175175
return urlsafe_base64_encode('{0}:{1}'.format(bucket, key))
176176

177177

178+
def decode_entry(e):
179+
return (s(urlsafe_base64_decode(e)).split(':') + [None] * 2)[:2]
180+
181+
178182
def rfc_from_timestamp(timestamp):
179183
"""将时间戳转换为HTTP RFC格式
180184

test_qiniu.py

+85-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from qiniu import put_data, put_file, put_stream
1717
from qiniu import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, build_batch_stat, \
1818
build_batch_delete, DomainManager
19-
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode, canonical_mime_header_key
19+
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode, canonical_mime_header_key, entry, decode_entry
2020

2121
from qiniu.compat import is_py2, is_py3, b, json
2222

@@ -255,6 +255,90 @@ def test_canonical_mime_header_key(self):
255255
for i in range(len(field_names)):
256256
assert canonical_mime_header_key(field_names[i]) == expect_canonical_field_names[i]
257257

258+
def test_entry(self):
259+
case_list = [
260+
{
261+
'msg': 'normal',
262+
'bucket': 'qiniuphotos',
263+
'key': 'gogopher.jpg',
264+
'expect': 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'
265+
},
266+
{
267+
'msg': 'key empty',
268+
'bucket': 'qiniuphotos',
269+
'key': '',
270+
'expect': 'cWluaXVwaG90b3M6'
271+
},
272+
{
273+
'msg': 'key undefined',
274+
'bucket': 'qiniuphotos',
275+
'key': None,
276+
'expect': 'cWluaXVwaG90b3M='
277+
},
278+
{
279+
'msg': 'key need replace plus symbol',
280+
'bucket': 'qiniuphotos',
281+
'key': '012ts>a',
282+
'expect': 'cWluaXVwaG90b3M6MDEydHM-YQ=='
283+
},
284+
{
285+
'msg': 'key need replace slash symbol',
286+
'bucket': 'qiniuphotos',
287+
'key': '012ts?a',
288+
'expect': 'cWluaXVwaG90b3M6MDEydHM_YQ=='
289+
}
290+
]
291+
for c in case_list:
292+
assert c.get('expect') == entry(c.get('bucket'), c.get('key')), c.get('msg')
293+
294+
def test_decode_entry(self):
295+
case_list = [
296+
{
297+
'msg': 'normal',
298+
'expect': {
299+
'bucket': 'qiniuphotos',
300+
'key': 'gogopher.jpg'
301+
},
302+
'entry': 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'
303+
},
304+
{
305+
'msg': 'key empty',
306+
'expect': {
307+
'bucket': 'qiniuphotos',
308+
'key': ''
309+
},
310+
'entry': 'cWluaXVwaG90b3M6'
311+
},
312+
{
313+
'msg': 'key undefined',
314+
'expect': {
315+
'bucket': 'qiniuphotos',
316+
'key': None
317+
},
318+
'entry': 'cWluaXVwaG90b3M='
319+
},
320+
{
321+
'msg': 'key need replace plus symbol',
322+
'expect': {
323+
'bucket': 'qiniuphotos',
324+
'key': '012ts>a'
325+
},
326+
'entry': 'cWluaXVwaG90b3M6MDEydHM-YQ=='
327+
},
328+
{
329+
'msg': 'key need replace slash symbol',
330+
'expect': {
331+
'bucket': 'qiniuphotos',
332+
'key': '012ts?a'
333+
},
334+
'entry': 'cWluaXVwaG90b3M6MDEydHM_YQ=='
335+
}
336+
]
337+
for c in case_list:
338+
bucket, key = decode_entry(c.get('entry'))
339+
assert bucket == c.get('expect').get('bucket'), c.get('msg')
340+
assert key == c.get('expect').get('key'), c.get('msg')
341+
258342

259343
class AuthTestCase(unittest.TestCase):
260344
def test_token(self):

0 commit comments

Comments
 (0)