Skip to content

Commit 8f00a5d

Browse files
authored
Merge pull request #280 from l-iberty/master
实现post_bucket_inventory
2 parents 0633969 + cf7c570 commit 8f00a5d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

qcloud_cos/cos_client.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,72 @@ def list_bucket_inventory_configurations(self, Bucket, ContinuationToken=None, *
30423042
data = xml_to_dict(rt.content)
30433043
return data
30443044

3045+
def post_bucket_inventory(self, Bucket, Id, InventoryConfiguration={}, **kwargs):
3046+
"""设置bucket的清单规则(一次性清单/即时清单)
3047+
3048+
:param Bucket(string): 存储桶名称.
3049+
:param Id(string): 清单规则名称.
3050+
:param InventoryConfiguration(dict): Bucket的清单规则.
3051+
:param kwargs(dict): 设置请求headers.
3052+
:return: None.
3053+
3054+
.. code-block:: python
3055+
3056+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3057+
client = CosS3Client(config)
3058+
# 设置bucket清单规则
3059+
inventory_config = {
3060+
'Destination': {
3061+
'COSBucketDestination': {
3062+
'AccountId': '100000000001',
3063+
'Bucket': 'qcs::cos:ap-guangzhou::examplebucket-1250000000',
3064+
'Format': 'CSV',
3065+
'Prefix': 'list1',
3066+
'Encryption': {
3067+
'SSECOS': {}
3068+
}
3069+
},
3070+
'Filter': {
3071+
'Prefix': 'filterPrefix'
3072+
},
3073+
'IncludedObjectVersions':'All',
3074+
'OptionalFields': {
3075+
'Field': [
3076+
'Size',
3077+
'LastModifiedDate',
3078+
'ETag',
3079+
'StorageClass',
3080+
'IsMultipartUploaded',
3081+
'ReplicationStatus'
3082+
]
3083+
},
3084+
}
3085+
response = client.put_bucket_inventory(
3086+
Bucket='bucket',
3087+
Id='list1',
3088+
InventoryConfiguration=inventory_config
3089+
)
3090+
"""
3091+
InventoryConfiguration['Id'] = Id
3092+
xml_config = format_xml(data=InventoryConfiguration, root='InventoryConfiguration')
3093+
headers = mapped(kwargs)
3094+
headers['Content-MD5'] = get_md5(xml_config)
3095+
headers['Content-Type'] = 'application/xml'
3096+
params = {'inventory': '', 'id': Id}
3097+
url = self._conf.uri(bucket=Bucket)
3098+
logger.info("post bucket inventory, url=:{url} ,headers=:{headers}".format(
3099+
url=url,
3100+
headers=headers))
3101+
rt = self.send_request(
3102+
method='POST',
3103+
url=url,
3104+
bucket=Bucket,
3105+
data=xml_config,
3106+
auth=CosS3Auth(self._conf, params=params),
3107+
headers=headers,
3108+
params=params)
3109+
return None
3110+
30453111
def put_object_tagging(self, Bucket, Key, Tagging={}, **kwargs):
30463112
"""设置object的标签
30473113

ut/test.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,41 @@ def test_list_bucket_inventory_configrations():
18011801
Id=id,
18021802
)
18031803

1804+
def test_post_bucket_inventory_configurations():
1805+
"""测试一次性/即时清单"""
1806+
inventory_config = {
1807+
'Destination': {
1808+
'COSBucketDestination': {
1809+
'AccountId': '2832742109',
1810+
'Bucket': 'qcs::cos:' + REGION + '::' + test_bucket,
1811+
'Format': 'CSV',
1812+
'Prefix': 'list1',
1813+
'Encryption': {
1814+
'SSECOS': {}
1815+
}
1816+
}
1817+
},
1818+
'Filter': {
1819+
'Prefix': 'filterPrefix'
1820+
},
1821+
'IncludedObjectVersions': 'All',
1822+
'OptionalFields': {
1823+
'Field': [
1824+
'Size',
1825+
'LastModifiedDate',
1826+
'ETag',
1827+
'StorageClass',
1828+
'IsMultipartUploaded',
1829+
'ReplicationStatus'
1830+
]
1831+
},
1832+
}
1833+
response = client.post_bucket_inventory(
1834+
Bucket=test_bucket,
1835+
Id='list1',
1836+
InventoryConfiguration=inventory_config,
1837+
)
1838+
18041839

18051840
def test_put_get_delete_bucket_tagging():
18061841
"""测试设置获取删除bucket标签"""
@@ -5292,6 +5327,7 @@ def test_meta_insight():
52925327

52935328
if __name__ == "__main__":
52945329
setUp()
5330+
test_post_bucket_inventory_configurations()
52955331
"""
52965332
test_config_invalid_scheme()
52975333
test_config_credential_inst()

0 commit comments

Comments
 (0)