Skip to content

Commit

Permalink
Compatible with old version bucketInfo API
Browse files Browse the repository at this point in the history
  • Loading branch information
huiguangjun committed Dec 25, 2019
1 parent 124afea commit cdc508a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
12 changes: 10 additions & 2 deletions oss2/xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,14 @@ def parse_get_bucket_info(result, body):
result.location = _find_tag(root, 'Bucket/Location')
result.owner = Owner(_find_tag(root, 'Bucket/Owner/DisplayName'), _find_tag(root, 'Bucket/Owner/ID'))
result.acl = AccessControlList(_find_tag(root, 'Bucket/AccessControlList/Grant'))
result.data_redundancy_type = _find_tag(root, 'Bucket/DataRedundancyType')
result.comment = _find_tag(root, 'Bucket/Comment')

server_side_encryption = root.find("Bucket/ServerSideEncryptionRule")

result.bucket_encryption_rule = _parse_bucket_encryption_info(server_side_encryption)
if server_side_encryption is None:
result.bucket_encryption_rule = None
else:
result.bucket_encryption_rule = _parse_bucket_encryption_info(server_side_encryption)

bucket_versioning = root.find('Bucket/Versioning')

Expand All @@ -310,6 +312,12 @@ def parse_get_bucket_info(result, body):
else:
result.versioning_status = to_string(bucket_versioning.text)

data_redundancy_type = root.find('Bucket/DataRedundancyType')
if data_redundancy_type is None or data_redundancy_type.text is None:
result.data_redundancy_type = None
else:
result.data_redundancy_type = to_string(data_redundancy_type.text)

return result

def _parse_bucket_encryption_info(node):
Expand Down
38 changes: 38 additions & 0 deletions unittests/test_xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import unittest
import xml.etree.ElementTree as ElementTree
from oss2.xml_utils import _find_tag, _find_bool
from oss2.xml_utils import parse_get_bucket_info
from .common import MockResponse
import oss2


class TestXmlUtils(unittest.TestCase):
Expand Down Expand Up @@ -35,6 +38,41 @@ def test_find_bool(self):

self.assertRaises(RuntimeError, _find_bool, root, 'none_exist_tag')

def test_parse_get_bucket_info(self):
body = '''
<BucketInfo>
<Bucket>
<CreationDate>2013-07-31T10:56:21.000Z</CreationDate>
<ExtranetEndpoint>oss-cn-hangzhou.aliyuncs.com</ExtranetEndpoint>
<IntranetEndpoint>oss-cn-hangzhou-internal.aliyuncs.com</IntranetEndpoint>
<Location>oss-cn-hangzhou</Location>
<Name>oss-example</Name>
<StorageClass>IA</StorageClass>
<Owner>
<DisplayName>username</DisplayName>
<ID>27183473914****</ID>
</Owner>
<AccessControlList>
<Grant>private</Grant>
</AccessControlList>
<Comment>test</Comment>
</Bucket>
</BucketInfo>
'''
headers = oss2.CaseInsensitiveDict({
'Server': 'AliyunOSS',
'Date': 'Fri, 11 Dec 2015 11:40:30 GMT',
'Content-Length': len(body),
'Connection': 'keep-alive',
'x-oss-request-id': '566AB62EB06147681C283D73',
'ETag': '7AE1A589ED6B161CAD94ACDB98206DA6'
})
resp = MockResponse(200, headers, body)

result = oss2.models.GetBucketInfoResult(resp)
parse_get_bucket_info(result, body)
self.assertEqual(result.location, 'oss-cn-hangzhou')


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

0 comments on commit cdc508a

Please sign in to comment.