Skip to content

Commit

Permalink
add path style domain name format (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiaolong37 authored and huiguangjun committed Sep 18, 2023
1 parent 2ecc543 commit e165360
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
27 changes: 17 additions & 10 deletions oss2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def progress_callback(bytes_consumed, total_bytes):

class _Base(object):
def __init__(self, auth, endpoint, is_cname, session, connect_timeout,
app_name='', enable_crc=True, proxies=None, region=None, cloudbox_id= None):
app_name='', enable_crc=True, proxies=None, region=None, cloudbox_id= None, is_path_style=False):
self.auth = auth
self.endpoint = _normalize_endpoint(endpoint.strip())
if utils.is_valid_endpoint(self.endpoint) is not True:
Expand All @@ -218,7 +218,7 @@ def __init__(self, auth, endpoint, is_cname, session, connect_timeout,
self.cloudbox_id = cloudbox_id
if self.cloudbox_id is not None:
self.product = 'oss-cloudbox'
self._make_url = _UrlMaker(self.endpoint, is_cname)
self._make_url = _UrlMaker(self.endpoint, is_cname, is_path_style)


def _do(self, method, bucket_name, key, **kwargs):
Expand Down Expand Up @@ -304,12 +304,13 @@ def __init__(self, auth, endpoint,
app_name='',
proxies=None,
region=None,
cloudbox_id=None):
cloudbox_id=None,
is_path_style=False):
logger.debug("Init oss service, endpoint: {0}, connect_timeout: {1}, app_name: {2}, proxies: {3}".format(
endpoint, connect_timeout, app_name, proxies))
super(Service, self).__init__(auth, endpoint, False, session, connect_timeout,
app_name=app_name, proxies=proxies,
region=region, cloudbox_id=cloudbox_id)
region=region, cloudbox_id=cloudbox_id, is_path_style=is_path_style)

def list_buckets(self, prefix='', marker='', max_keys=100, params=None, headers=None):
"""根据前缀罗列用户的Bucket。
Expand Down Expand Up @@ -444,12 +445,13 @@ def __init__(self, auth, endpoint, bucket_name,
enable_crc=True,
proxies=None,
region=None,
cloudbox_id=None):
cloudbox_id=None,
is_path_style=False):
logger.debug("Init Bucket: {0}, endpoint: {1}, isCname: {2}, connect_timeout: {3}, app_name: {4}, enabled_crc: {5}, region: {6}"
", proxies: {6}".format(bucket_name, endpoint, is_cname, connect_timeout, app_name, enable_crc, proxies, region))
super(Bucket, self).__init__(auth, endpoint, is_cname, session, connect_timeout,
app_name=app_name, enable_crc=enable_crc, proxies=proxies,
region=region, cloudbox_id=cloudbox_id)
region=region, cloudbox_id=cloudbox_id, is_path_style=is_path_style)

self.bucket_name = bucket_name.strip()
if utils.is_valid_bucket_name(self.bucket_name) is not True:
Expand Down Expand Up @@ -2864,6 +2866,7 @@ def _normalize_endpoint(endpoint):
_ENDPOINT_TYPE_ALIYUN = 0
_ENDPOINT_TYPE_CNAME = 1
_ENDPOINT_TYPE_IP = 2
_ENDPOINT_TYPE_PATH_STYLE = 3


def _make_range_string(range):
Expand All @@ -2889,37 +2892,41 @@ def to_str(pos):
return to_str(start) + '-' + to_str(last)


def _determine_endpoint_type(netloc, is_cname, bucket_name):
def _determine_endpoint_type(netloc, is_cname, bucket_name, is_path_style):
if utils.is_ip_or_localhost(netloc):
return _ENDPOINT_TYPE_IP

if is_cname:
return _ENDPOINT_TYPE_CNAME

if is_path_style:
return _ENDPOINT_TYPE_PATH_STYLE

if utils.is_valid_bucket_name(bucket_name):
return _ENDPOINT_TYPE_ALIYUN
else:
return _ENDPOINT_TYPE_IP


class _UrlMaker(object):
def __init__(self, endpoint, is_cname):
def __init__(self, endpoint, is_cname, is_path_style):
p = urlparse(endpoint)

self.scheme = p.scheme
self.netloc = p.netloc
self.is_cname = is_cname
self.is_path_style = is_path_style

def __call__(self, bucket_name, key, slash_safe=False):
self.type = _determine_endpoint_type(self.netloc, self.is_cname, bucket_name)
self.type = _determine_endpoint_type(self.netloc, self.is_cname, bucket_name, self.is_path_style)

safe = '/' if slash_safe is True else ''
key = urlquote(key, safe=safe)

if self.type == _ENDPOINT_TYPE_CNAME:
return '{0}://{1}/{2}'.format(self.scheme, self.netloc, key)

if self.type == _ENDPOINT_TYPE_IP:
if self.type == _ENDPOINT_TYPE_IP or self.type == _ENDPOINT_TYPE_PATH_STYLE:
if bucket_name:
return '{0}://{1}/{2}/{3}'.format(self.scheme, self.netloc, bucket_name, key)
else:
Expand Down
29 changes: 29 additions & 0 deletions tests/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,5 +1287,34 @@ def test_list_buckets_with_region_list(self):
self.assertEqual(200, result.status)
self.assertTrue(result.buckets.__len__() > 0)

def test_bucket_path_style(self):
auth = oss2.Auth(OSS_ID, OSS_SECRET)
bucket = oss2.Bucket(auth, OSS_ENDPOINT, self.OSS_BUCKET)
bucket2 = oss2.Bucket(auth, OSS_ENDPOINT, self.OSS_BUCKET, is_path_style=True)

service = oss2.Service(auth, OSS_ENDPOINT)
service2 = oss2.Service(auth, OSS_ENDPOINT, is_path_style=True)

try:
result = bucket.get_bucket_acl()
self.assertEqual(200, result.status)
self.assertEqual('private', result.acl)

bucket2.get_bucket_acl()
except oss2.exceptions.OssError as e:
self.assertEqual(e.code, 'SecondLevelDomainForbidden')

try:
params = {}
params['regionList']=''
result = service.list_buckets(params=params)
self.assertEqual(200, result.status)
self.assertTrue(result.buckets.__len__() > 0)

service2.list_buckets(params=params)
except oss2.exceptions.OssError as e:
self.assertEqual(e.code, 'SecondLevelDomainForbidden')


if __name__ == '__main__':
unittest.main()
28 changes: 28 additions & 0 deletions unittests/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -3097,5 +3097,33 @@ def test_put_async_fetch_callback_failed(self, do_request):
self.assertRequest(req_info, request_text.format(str(callback_when_failed).lower()))


@patch('oss2.Session.do_request')
def test_path_style(self, do_request):
request_text = '''DELETE /ming-oss-share/?policy&comp=callback HTTP/1.1
Host: oss-cn-hangzhou.aliyuncs.com
Accept-Encoding: identity
Connection: keep-alive
date: Sat, 12 Dec 2015 00:35:41 GMT
User-Agent: aliyun-sdk-python/2.0.2(Windows/7/;3.3.3)
Accept: */*
authorization: OSS ZCDmm7TPZKHtx77j:Pt0DtPQ/FODOGs5y0yTIVctRcok='''

response_text = '''HTTP/1.1 204 OK
Server: AliyunOSS
Date: Sat, 12 Dec 2015 00:35:42 GMT
Content-Type: application/xml
Content-Length: 96
Connection: keep-alive
x-oss-request-id: 566B6BDD68248CE14F729DC0
'''
req_info = mock_response(do_request, response_text)
bucket = oss2.Bucket(oss2.Auth('fake-access-key-id', 'fake-access-key-secret'),
'http://oss-cn-hangzhou.aliyuncs.com', BUCKET_NAME, is_path_style=True)
result = bucket.delete_bucket_callback_policy()

self.assertRequest(req_info, request_text)
self.assertEqual(result.status, 204)


if __name__ == '__main__':
unittest.main()

0 comments on commit e165360

Please sign in to comment.