Skip to content

Commit 6a009c4

Browse files
author
libertyzhu
committed
head_bucket添加返回值; list_bucket_inventory_configurations接口
1 parent ce45a6b commit 6a009c4

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

qcloud_cos/cos_client.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
4444
Access_id=None, Access_key=None, Secret_id=None, Secret_key=None, Endpoint=None, IP=None, Port=None,
4545
Anonymous=None, UA=None, Proxies=None, Domain=None, ServiceDomain=None, KeepAlive=True, PoolConnections=10,
4646
PoolMaxSize=10, AllowRedirects=False, SignHost=True, EndpointCi=None, EndpointPic=None, EnableOldDomain=True, EnableInternalDomain=True, SignParams=True,
47-
AutoSwitchDomainOnRetry=True):
47+
AutoSwitchDomainOnRetry=False):
4848
"""初始化,保存用户的信息
4949
5050
:param Appid(string): 用户APPID.
@@ -1635,7 +1635,7 @@ def head_bucket(self, Bucket, **kwargs):
16351635
bucket=Bucket,
16361636
auth=CosS3Auth(self._conf),
16371637
headers=headers)
1638-
return None
1638+
return rt.headers
16391639

16401640
def put_bucket_acl(self, Bucket, AccessControlPolicy={}, **kwargs):
16411641
"""设置bucket ACL
@@ -2926,7 +2926,7 @@ def get_bucket_inventory(self, Bucket, Id, **kwargs):
29262926
return data
29272927

29282928
def delete_bucket_inventory(self, Bucket, Id, **kwargs):
2929-
"""删除bucket 回源配置
2929+
"""删除bucket清单规则
29302930
29312931
:param Bucket(string): 存储桶名称.
29322932
:param Id(string): 清单规则名称.
@@ -2957,6 +2957,52 @@ def delete_bucket_inventory(self, Bucket, Id, **kwargs):
29572957
params=params)
29582958
return None
29592959

2960+
def list_bucket_inventory_configurations(self, Bucket, ContinuationToken=None, **kwargs):
2961+
"""列举存储桶清单规则
2962+
2963+
:param Bucket(string): 存储桶名称
2964+
:param ContinuationToken(string): 分页参数, 用以获取下一页信息
2965+
:param kwargs(dict): 设置请求headers.
2966+
:return(dict): 存储桶清单规则列表
2967+
2968+
.. code-block:: python
2969+
2970+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
2971+
client = CosS3Client(config)
2972+
# 分页列举bucket清单规则
2973+
continuation_token = ''
2974+
while True:
2975+
resp = client.list_bucket_inventory_configurations(
2976+
Bucket=bucket,
2977+
ContinuationToken=continuation_token,
2978+
)
2979+
if 'InventoryConfiguration' in resp:
2980+
for conf in resp['InventoryConfiguration']:
2981+
id = 'ID-{}'.format(i)
2982+
if resp['IsTruncated'] == 'true':
2983+
continuation_token = resp['NextContinuationToken']
2984+
else:
2985+
break
2986+
"""
2987+
headers = mapped(kwargs)
2988+
params = {'inventory': ''}
2989+
if ContinuationToken is not None:
2990+
params['continuation-token'] = ContinuationToken
2991+
url = self._conf.uri(bucket=Bucket)
2992+
logger.info("list bucket inventory configurations, url={url}, headers={headers}".format(
2993+
url=url,
2994+
headers=headers,
2995+
))
2996+
rt = self.send_request(
2997+
method='GET',
2998+
url=url,
2999+
bucket=Bucket,
3000+
auth=CosS3Auth(self._conf, params=params),
3001+
headers=headers,
3002+
params=params)
3003+
data = xml_to_dict(rt.content)
3004+
return data
3005+
29603006
def put_object_tagging(self, Bucket, Key, Tagging={}, **kwargs):
29613007
"""设置object的标签
29623008

ut/test.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,76 @@ def test_put_get_delete_bucket_inventory():
15061506
Id='test'
15071507
)
15081508

1509+
def test_list_bucket_inventory_configrations():
1510+
"""测试列举bucket清单"""
1511+
inventory_config = {
1512+
'Destination': {
1513+
'COSBucketDestination': {
1514+
'AccountId': '2832742109',
1515+
'Bucket': 'qcs::cos:' + REGION + '::' + test_bucket,
1516+
'Format': 'CSV',
1517+
'Prefix': 'list1',
1518+
'Encryption': {
1519+
'SSECOS': {}
1520+
}
1521+
}
1522+
},
1523+
'IsEnabled': 'True',
1524+
'Filter': {
1525+
'Prefix': 'filterPrefix'
1526+
},
1527+
'IncludedObjectVersions': 'All',
1528+
'OptionalFields': {
1529+
'Field': [
1530+
'Size',
1531+
'LastModifiedDate',
1532+
'ETag',
1533+
'StorageClass',
1534+
'IsMultipartUploaded',
1535+
'ReplicationStatus'
1536+
]
1537+
},
1538+
'Schedule': {
1539+
'Frequency': 'Daily'
1540+
}
1541+
}
1542+
# 构建150条清单配置规则(清单分页大小为100条规则)
1543+
n = 150
1544+
for i in range(n):
1545+
id = 'ID-{}'.format(i)
1546+
response = client.put_bucket_inventory(
1547+
Bucket=test_bucket,
1548+
Id=id,
1549+
InventoryConfiguration=inventory_config,
1550+
)
1551+
1552+
# 列举清单
1553+
i = 0
1554+
continuation_token = ''
1555+
while True:
1556+
resp = client.list_bucket_inventory_configurations(
1557+
Bucket=test_bucket,
1558+
ContinuationToken=continuation_token,
1559+
)
1560+
if 'InventoryConfiguration' in resp:
1561+
for conf in resp['InventoryConfiguration']:
1562+
id = 'ID-{}'.format(i)
1563+
assert id == conf['Id']
1564+
i += 1
1565+
if resp['IsTruncated'] == 'true':
1566+
continuation_token = resp['NextContinuationToken']
1567+
else:
1568+
break
1569+
1570+
assert i == n
1571+
1572+
# 删除清单
1573+
for i in range(n):
1574+
id = 'ID-{}'.format(i)
1575+
response = client.delete_bucket_inventory(
1576+
Bucket=test_bucket,
1577+
Id=id,
1578+
)
15091579

15101580
def test_put_get_delete_bucket_tagging():
15111581
"""测试设置获取删除bucket标签"""

0 commit comments

Comments
 (0)