Skip to content

Commit

Permalink
fix batch rs host (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored Dec 11, 2023
1 parent 4d54f21 commit cb0d415
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
2 changes: 1 addition & 1 deletion qiniu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
from .services.compute.qcos_api import QcosClient
from .services.sms.sms import Sms
from .services.pili.rtc_server_manager import RtcServer, get_room_token
from .utils import urlsafe_base64_encode, urlsafe_base64_decode, etag, entry, canonical_mime_header_key
from .utils import urlsafe_base64_encode, urlsafe_base64_decode, etag, entry, decode_entry, canonical_mime_header_key
17 changes: 15 additions & 2 deletions qiniu/services/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from qiniu import config, QiniuMacAuth
from qiniu import http
from qiniu.utils import urlsafe_base64_encode, entry
from qiniu.utils import urlsafe_base64_encode, entry, decode_entry


class BucketManager(object):
Expand Down Expand Up @@ -344,7 +344,20 @@ def batch(self, operations):
]
一个ResponseInfo对象
"""
url = '{0}/batch'.format(config.get_default('default_rs_host'))
if not operations:
raise Exception('operations is empty')
bucket = ''
for op in operations:
segments = op.split('/')
e = segments[1] if len(segments) >= 2 else ''
bucket, _ = decode_entry(e)
if bucket:
break
if not bucket:
raise Exception('bucket is empty')
ak = self.auth.get_access_key()
rs_host = self.zone.get_rs_host(ak, bucket)
url = '{0}/batch'.format(rs_host)
return self.__post(url, dict(op=operations))

def buckets(self):
Expand Down
4 changes: 4 additions & 0 deletions qiniu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ def entry(bucket, key):
return urlsafe_base64_encode('{0}:{1}'.format(bucket, key))


def decode_entry(e):
return (s(urlsafe_base64_decode(e)).split(':') + [None] * 2)[:2]


def rfc_from_timestamp(timestamp):
"""将时间戳转换为HTTP RFC格式
Expand Down
86 changes: 85 additions & 1 deletion test_qiniu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from qiniu import put_data, put_file, put_stream
from qiniu import BucketManager, build_batch_copy, build_batch_rename, build_batch_move, build_batch_stat, \
build_batch_delete, DomainManager
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode, canonical_mime_header_key
from qiniu import urlsafe_base64_encode, urlsafe_base64_decode, canonical_mime_header_key, entry, decode_entry

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

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

def test_entry(self):
case_list = [
{
'msg': 'normal',
'bucket': 'qiniuphotos',
'key': 'gogopher.jpg',
'expect': 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'
},
{
'msg': 'key empty',
'bucket': 'qiniuphotos',
'key': '',
'expect': 'cWluaXVwaG90b3M6'
},
{
'msg': 'key undefined',
'bucket': 'qiniuphotos',
'key': None,
'expect': 'cWluaXVwaG90b3M='
},
{
'msg': 'key need replace plus symbol',
'bucket': 'qiniuphotos',
'key': '012ts>a',
'expect': 'cWluaXVwaG90b3M6MDEydHM-YQ=='
},
{
'msg': 'key need replace slash symbol',
'bucket': 'qiniuphotos',
'key': '012ts?a',
'expect': 'cWluaXVwaG90b3M6MDEydHM_YQ=='
}
]
for c in case_list:
assert c.get('expect') == entry(c.get('bucket'), c.get('key')), c.get('msg')

def test_decode_entry(self):
case_list = [
{
'msg': 'normal',
'expect': {
'bucket': 'qiniuphotos',
'key': 'gogopher.jpg'
},
'entry': 'cWluaXVwaG90b3M6Z29nb3BoZXIuanBn'
},
{
'msg': 'key empty',
'expect': {
'bucket': 'qiniuphotos',
'key': ''
},
'entry': 'cWluaXVwaG90b3M6'
},
{
'msg': 'key undefined',
'expect': {
'bucket': 'qiniuphotos',
'key': None
},
'entry': 'cWluaXVwaG90b3M='
},
{
'msg': 'key need replace plus symbol',
'expect': {
'bucket': 'qiniuphotos',
'key': '012ts>a'
},
'entry': 'cWluaXVwaG90b3M6MDEydHM-YQ=='
},
{
'msg': 'key need replace slash symbol',
'expect': {
'bucket': 'qiniuphotos',
'key': '012ts?a'
},
'entry': 'cWluaXVwaG90b3M6MDEydHM_YQ=='
}
]
for c in case_list:
bucket, key = decode_entry(c.get('entry'))
assert bucket == c.get('expect').get('bucket'), c.get('msg')
assert key == c.get('expect').get('key'), c.get('msg')


class AuthTestCase(unittest.TestCase):
def test_token(self):
Expand Down

0 comments on commit cb0d415

Please sign in to comment.