def test_object_iterator_dir(self, do_request): body_list = [ b'''<?xml version="1.0" encoding="UTF-8"?> <ListBucketResult> <Name>ming-spike</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <Delimiter>%2F</Delimiter> <EncodingType>url</EncodingType> <IsTruncated>false</IsTruncated> <Contents> <Key>a.txt</Key> <LastModified>2016-01-07T11:10:00.000Z</LastModified> <ETag>"5EB63BBBE01EEED093CB22BB8F5ACDC3"</ETag> <Type>Normal</Type> <Size>11</Size> <StorageClass>Standard</StorageClass> <Owner> <ID>1047205513514293</ID> <DisplayName>1047205513514293</DisplayName> </Owner> </Contents> <CommonPrefixes> <Prefix>%E6%96%87%E4%BB%B6%2F</Prefix> </CommonPrefixes> </ListBucketResult>''' ] expected = [ SimplifiedObjectInfo('a.txt', 1452165000, '5EB63BBBE01EEED093CB22BB8F5ACDC3', 'Normal', 11, 'Standard', Owner('1047205513514293', '1047205513514293')), SimplifiedObjectInfo('文件/', None, None, None, None, None, None) ] req_info = RequestInfo() do_request.auto_spec = True do_request.side_effect = make_do4body(req_infos=[req_info], body_list=body_list) got = list(oss2.ObjectIterator(bucket(), max_keys=1000)) for i in range(len(expected)): self.assertSimpleObjectInfoEqual(expected[i], got[i])
async def _fetch(self): result = await self.bucket.list_objects(prefix=self.prefix, delimiter=self.delimiter, marker=self.next_marker, max_keys=self.max_keys) self.entries = result.object_list + [ SimplifiedObjectInfo(prefix, None, None, None, None, None) for prefix in result.prefix_list ] self.entries.sort(key=lambda obj: obj.key) return result.is_truncated, result.next_marker
def test_handler(self): with mock.patch('index.oss2') as mock_oss: with mock.patch.dict('os.environ', {'SRC_OSS_ENDPOINT': 'ep'}): resp = mock.Mock() lor = ListObjectsResult(resp) lor.is_truncated = False lor.marker = "" lor.object_list = [ SimplifiedObjectInfo("10", 1, "", "", 10, ""), SimplifiedObjectInfo("15", 1, "", "", 15, ""), SimplifiedObjectInfo("8", 1, "", "", 8, ""), SimplifiedObjectInfo("5", 1, "", "", 5, ""), SimplifiedObjectInfo("5", 1, "", "", 5, ""), SimplifiedObjectInfo("4", 1, "", "", 4, ""), ] mock_oss.Bucket.return_value.list_objects.return_value = lor event = """ { "bucket": "hangzhouhangzhou", "prefix": "", "marker": "", "delimiter": "/", "small_file_limit": 5, "medium_file_limit": 10, "max_group_size": 5 } """ context = mock.Mock() result = handler(event, context) self.assertEqual( result, { "has_more": True, "marker": "4", "small": [["5", "5"]], "medium": [["10", 10], ["8", 8]], "large": [["15", 15]] })
def test_object_iterator_truncated(self, do_request): body_list = [ b'''<?xml version="1.0" encoding="UTF-8"?> <ListBucketResult> <Name>ming-spike</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1</MaxKeys> <Delimiter></Delimiter> <EncodingType>url</EncodingType> <IsTruncated>true</IsTruncated> <NextMarker>a.txt</NextMarker> <Contents> <Key>a.txt</Key> <LastModified>2016-01-07T11:10:00.000Z</LastModified> <ETag>"5EB63BBBE01EEED093CB22BB8F5ACDC3"</ETag> <Type>Normal</Type> <Size>11</Size> <StorageClass>Standard</StorageClass> <Owner> <ID>1047205513514293</ID> <DisplayName>1047205513514293</DisplayName> </Owner> </Contents> </ListBucketResult>''', b'''<?xml version="1.0" encoding="UTF-8"?> <ListBucketResult> <Name>ming-spike</Name> <Prefix></Prefix> <Marker>a.txt</Marker> <MaxKeys>1</MaxKeys> <Delimiter></Delimiter> <EncodingType>url</EncodingType> <IsTruncated>true</IsTruncated> <NextMarker>%E4%B8%AD%E6%96%87.txt</NextMarker> <Contents> <Key>%E4%B8%AD%E6%96%87.txt</Key> <LastModified>2016-01-07T11:09:39.000Z</LastModified> <ETag>"FC3FF98E8C6A0D3087D515C0473F8677"</ETag> <Type>Normal</Type> <Size>12</Size> <StorageClass>Standard</StorageClass> <Owner> <ID>1047205513514293</ID> <DisplayName>1047205513514293</DisplayName> </Owner> </Contents> </ListBucketResult>''', b'''<?xml version="1.0" encoding="UTF-8"?> <ListBucketResult> <Name>ming-spike</Name> <Prefix></Prefix> <Marker>%E4%B8%AD%E6%96%87.txt</Marker> <MaxKeys>1</MaxKeys> <Delimiter></Delimiter> <EncodingType>url</EncodingType> <IsTruncated>false</IsTruncated> <Contents> <Key>%E9%98%BF%E9%87%8C%E4%BA%91.txt</Key> <LastModified>2016-01-07T11:07:32.000Z</LastModified> <ETag>"5D41402ABC4B2A76B9719D911017C592"</ETag> <Type>Normal</Type> <Size>5</Size> <StorageClass>Standard</StorageClass> <Owner> <ID>1047205513514293</ID> <DisplayName>1047205513514293</DisplayName> </Owner> </Contents> </ListBucketResult>''' ] expected = [ SimplifiedObjectInfo('a.txt', 1452165000, '5EB63BBBE01EEED093CB22BB8F5ACDC3', 'Normal', 11, 'Standard'), SimplifiedObjectInfo('中文.txt', 1452164979, 'FC3FF98E8C6A0D3087D515C0473F8677', 'Normal', 12, 'Standard'), SimplifiedObjectInfo('阿里云.txt', 1452164852, '5D41402ABC4B2A76B9719D911017C592', 'Normal', 5, 'Standard') ] nreq = 3 req_infos = [RequestInfo() for i in range(nreq)] do_request.auto_spec = True do_request.side_effect = make_do4body(req_infos=req_infos, body_list=body_list) got = list(oss2.ObjectIterator(bucket(), max_keys=1)) for i in range(len(expected)): self.assertInstanceEqual(expected[i], got[i]) for i in range(nreq): self.assertEqual(req_infos[i].req.params.get('prefix', ''), '') self.assertEqual(req_infos[i].req.params.get('max-keys', ''), '1') self.assertEqual(req_infos[i].req.params.get('delimiter', ''), '') self.assertEqual(req_infos[i].req.params.get('encoding-type', ''), 'url') self.assertEqual(req_infos[0].req.params.get('marker', ''), '') self.assertEqual(req_infos[1].req.params.get('marker', ''), 'a.txt') self.assertEqual(req_infos[2].req.params.get('marker', ''), '中文.txt')
def test_object_iterator_not_truncated(self, do_request): body_list = [ b'''<?xml version="1.0" encoding="UTF-8"?> <ListBucketResult> <Name>zzy-share</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <Delimiter></Delimiter> <IsTruncated>false</IsTruncated> <Contents> <Key>object-1</Key> <LastModified>2015-02-02T05:15:13.000Z</LastModified> <ETag>"716AF6FFD529DFEA856FAA4E12D2C5EA"</ETag> <Type>Normal</Type> <Size>4308</Size> <StorageClass>Standard</StorageClass> <Owner> <ID>1047205513514293</ID> <DisplayName>1047205513514293</DisplayName> </Owner> </Contents> <Contents> <Key>object-2</Key> <LastModified>2015-06-23T09:56:55.000Z</LastModified> <ETag>"333D74B47CB1B0E275D2AB3CDDA02665-26"</ETag> <Type>Multipart</Type> <Size>3389246</Size> <StorageClass>Standard</StorageClass> <Owner> <ID>1047205513514293</ID> <DisplayName>1047205513514293</DisplayName> </Owner> </Contents> <Contents> <Key>object-3</Key> <LastModified>2015-01-16T12:41:34.000Z</LastModified> <ETag>"B28F7255E6EA777DB0AFB1C58C2CFCFE"</ETag> <Type>Normal</Type> <Size>10718416</Size> <StorageClass>Standard</StorageClass> <Owner> <ID>1047205513514293</ID> <DisplayName>1047205513514293</DisplayName> </Owner> </Contents> </ListBucketResult> ''' ] req_info = RequestInfo() do_request.auto_spec = True do_request.side_effect = make_do4body(req_infos=[req_info], body_list=body_list) got = list(oss2.ObjectIterator(bucket(), max_keys=1000)) expected = [ SimplifiedObjectInfo('object-1', 1422854113, '716AF6FFD529DFEA856FAA4E12D2C5EA', 'Normal', 4308, 'Standard'), SimplifiedObjectInfo('object-2', 1435053415, '333D74B47CB1B0E275D2AB3CDDA02665-26', 'Multipart', 3389246, 'Standard'), SimplifiedObjectInfo('object-3', 1421412094, 'B28F7255E6EA777DB0AFB1C58C2CFCFE', 'Normal', 10718416, 'Standard') ] self.assertEqual(len(expected), len(got)) for i in range(len(expected)): self.assertInstanceEqual(expected[i], got[i]) self.assertEqual(req_info.req.params.get('prefix', ''), '') self.assertEqual(req_info.req.params.get('marker', ''), '') self.assertEqual(req_info.req.params.get('encoding-type'), 'url')