Skip to content

Commit da03de1

Browse files
authored
Merge pull request #289 from l-iberty/master
add api: get_bucket_meta, bucket IntelligentTiering v2
2 parents c93ea9a + b0825fa commit da03de1

File tree

2 files changed

+424
-3
lines changed

2 files changed

+424
-3
lines changed

qcloud_cos/cos_client.py

Lines changed: 322 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,11 +3464,54 @@ def delete_bucket_referer(self, Bucket, **kwargs):
34643464
params=params)
34653465
return None
34663466

3467+
def put_bucket_intelligenttiering_v2(self, Bucket, IntelligentTieringConfiguration=None, Id=None, **kwargs):
3468+
"""设置存储桶智能分层配置
3469+
3470+
:param Bucket(string): 存储桶名称.
3471+
:param IntelligentTieringConfiguration(dict): 智能分层配置
3472+
:param kwargs(dict): 设置请求headers.
3473+
:return: None.
3474+
3475+
.. code-block:: python
3476+
3477+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3478+
client = CosS3Client(config)
3479+
3480+
intelligent_tiering_conf = {
3481+
'Status': 'Enable',
3482+
'Transition': {
3483+
'Days': '30|60|90',
3484+
'RequestFrequent': '1'
3485+
}
3486+
}
3487+
client.put_bucket_intelligenttiering(Bucket="bucket", IntelligentTieringConfiguration=intelligent_tiering_conf)
3488+
"""
3489+
3490+
if IntelligentTieringConfiguration is None:
3491+
IntelligentTieringConfiguration = {}
3492+
xml_config = format_xml(data=IntelligentTieringConfiguration, root='IntelligentTieringConfiguration')
3493+
headers = mapped(kwargs)
3494+
headers['Content-Type'] = 'application/xml'
3495+
params = {'id': Id}
3496+
url = self._conf.uri(bucket=Bucket) + '?intelligent-tiering'
3497+
logger.info("put bucket intelligenttiering, url=:{url} ,headers=:{headers}".format(
3498+
url=url,
3499+
headers=headers))
3500+
rt = self.send_request(
3501+
method='PUT',
3502+
url=url,
3503+
bucket=Bucket,
3504+
data=xml_config,
3505+
auth=CosS3Auth(self._conf, params=params),
3506+
headers=headers,
3507+
params=params)
3508+
return None
3509+
34673510
def put_bucket_intelligenttiering(self, Bucket, IntelligentTieringConfiguration=None, **kwargs):
34683511
"""设置存储桶智能分层配置
34693512
34703513
:param Bucket(string): 存储桶名称.
3471-
:param IntelligentTieringConfiguration(dict): 只能分层配置
3514+
:param IntelligentTieringConfiguration(dict): 智能分层配置
34723515
:param kwargs(dict): 设置请求headers.
34733516
:return: None.
34743517
@@ -3510,7 +3553,6 @@ def put_bucket_intelligenttiering(self, Bucket, IntelligentTieringConfiguration=
35103553
def get_bucket_intelligenttiering(self, Bucket, **kwargs):
35113554
"""获取存储桶智能分层配置
35123555
:param Bucket(string): 存储桶名称.
3513-
:param IntelligentTieringConfiguration(dict): 智能分层配置
35143556
:param kwargs(dict): 设置请求headers.
35153557
:return(dict): 智能分层配置.
35163558
@@ -3536,6 +3578,284 @@ def get_bucket_intelligenttiering(self, Bucket, **kwargs):
35363578
data = xml_to_dict(rt.content)
35373579
return data
35383580

3581+
def get_bucket_intelligenttiering_v2(self, Bucket, Id, **kwargs):
3582+
"""获取存储桶智能分层配置
3583+
3584+
:param Bucket(string): 存储桶名称.
3585+
:param Id(string) 智能分层规则Id.
3586+
:param kwargs(dict): 设置请求headers.
3587+
:return(dict): 智能分层配置.
3588+
3589+
.. code-block:: python
3590+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3591+
client = CosS3Client(config)
3592+
client.get_bucket_intelligenttiering_v2(Bucket='bucket', Id='id')
3593+
"""
3594+
3595+
headers = mapped(kwargs)
3596+
params = {'id': Id}
3597+
url = self._conf.uri(bucket=Bucket) + '?intelligent-tiering'
3598+
logger.info("get bucket intelligenttiering, url=:{url} ,headers=:{headers}".format(
3599+
url=url,
3600+
headers=headers))
3601+
rt = self.send_request(
3602+
method='GET',
3603+
url=url,
3604+
bucket=Bucket,
3605+
auth=CosS3Auth(self._conf, params=params),
3606+
headers=headers,
3607+
params=params)
3608+
data = xml_to_dict(rt.content)
3609+
return data
3610+
3611+
def list_bucket_intelligenttiering_configurations(self, Bucket, **kwargs):
3612+
"""列举存储桶中的所有智能分层配置
3613+
3614+
:param Bucket(string): 存储桶名称.
3615+
:param kwargs(dict): 设置请求headers.
3616+
:return(dict): 所有的智能分层配置.
3617+
3618+
.. code-block:: python
3619+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3620+
client = CosS3Client(config)
3621+
client.list_bucket_intelligenttiering_configurations(Bucket='bucket')
3622+
"""
3623+
3624+
headers = mapped(kwargs)
3625+
params = {}
3626+
url = self._conf.uri(bucket=Bucket) + "?intelligent-tiering"
3627+
logger.info("list bucket intelligenttiering configurations, url=:{url} ,headers=:{headers}".format(
3628+
url=url,
3629+
headers=headers))
3630+
rt = self.send_request(
3631+
method='GET',
3632+
url=url,
3633+
bucket=Bucket,
3634+
auth=CosS3Auth(self._conf, params=params),
3635+
headers=headers,
3636+
params=params)
3637+
data = xml_to_dict(rt.content)
3638+
return data
3639+
3640+
def put_bucket_object_lock(self, Bucket, ObjectLockConfiguration={}, **kwargs):
3641+
"""设置存储桶对象锁定配置
3642+
3643+
:param Bucket(string): 存储桶名称.
3644+
:param ObjectLockConfiguration(dict): 对象锁定配置.
3645+
:param kwargs(dict): 设置请求headers.
3646+
:return: None.
3647+
3648+
.. code-block:: python
3649+
3650+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3651+
client = CosS3Client(config)
3652+
3653+
object_lock_conf = {
3654+
'ObjectLockEnabled': 'Enabled',
3655+
}
3656+
client.put_bucket_object_lock(Bucket="bucket", ObjectLockConfiguration=objeck_lock_conf)
3657+
"""
3658+
3659+
xml_config = format_xml(data=ObjectLockConfiguration, root='ObjectLockConfiguration')
3660+
headers = mapped(kwargs)
3661+
headers['Content-MD5'] = get_md5(xml_config)
3662+
headers['Content-Type'] = 'application/xml'
3663+
params = {'object-lock': ''}
3664+
url = self._conf.uri(bucket=Bucket)
3665+
logger.info("put bucket object-lock, url=:{url} ,headers=:{headers}".format(
3666+
url=url,
3667+
headers=headers))
3668+
rt = self.send_request(
3669+
method='PUT',
3670+
url=url,
3671+
bucket=Bucket,
3672+
data=xml_config,
3673+
auth=CosS3Auth(self._conf, params=params),
3674+
headers=headers,
3675+
params=params)
3676+
return rt.headers
3677+
3678+
def get_bucket_object_lock(self, Bucket, **kwargs):
3679+
"""获取存储桶对象锁定配置
3680+
3681+
:param Bucket(string): 存储桶名称.
3682+
:param kwargs(dict): 设置请求headers.
3683+
:return(dict): 对象锁定配置.
3684+
3685+
.. code-block:: python
3686+
3687+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3688+
client = CosS3Client(config)
3689+
client.get_bucket_object_lock(Bucket="bucket")
3690+
"""
3691+
headers = mapped(kwargs)
3692+
params = {'object-lock': ''}
3693+
url = self._conf.uri(bucket=Bucket)
3694+
logger.info("get bucket object-lock, url=:{url} ,headers=:{headers}".format(
3695+
url=url,
3696+
headers=headers))
3697+
rt = self.send_request(
3698+
method='GET',
3699+
url=url,
3700+
bucket=Bucket,
3701+
auth=CosS3Auth(self._conf, params=params),
3702+
headers=headers,
3703+
params=params)
3704+
data = xml_to_dict(rt.content)
3705+
return data
3706+
3707+
def get_bucket_meta(self, Bucket, **kwargs):
3708+
"""获取存储桶各项配置
3709+
3710+
:param Bucket(string): 存储桶名称.
3711+
:param kwargs(dict): 设置请求headers.
3712+
:return(dict): 存储桶各项配置.
3713+
3714+
.. code-block:: python
3715+
3716+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3717+
client = CosS3Client(config)
3718+
client.get_bucket_meta(Bucket="bucket")
3719+
"""
3720+
data = {
3721+
'BucketUrl': None,
3722+
'OFS': False,
3723+
'MAZ': False,
3724+
'Encryption': None,
3725+
'ACL': None,
3726+
'Website': None,
3727+
'Logging': None,
3728+
'CORS': None,
3729+
'Versioning': None,
3730+
'IntelligentTiering': None,
3731+
'Lifecycle': None,
3732+
'Tagging': None,
3733+
'ObjectLock': None,
3734+
'Replication': None,
3735+
}
3736+
pool = SimpleThreadPool(num_threads=10)
3737+
3738+
# HeadBucket
3739+
def _head_bucket_wrapper(Bucket, **kwargs):
3740+
resp = self.head_bucket(Bucket, **kwargs)
3741+
# x-cos-bucket-arch: 'OFS'
3742+
# x-cos-bucket-az-type: 'MAZ'
3743+
# x-cos-bucket-region: 'ap-beijing'
3744+
if 'x-cos-bucket-arch' in resp and resp['x-cos-bucket-arch'] == 'OFS':
3745+
data.update({'OFS': True})
3746+
else:
3747+
data.update({'OFS': False})
3748+
if 'x-cos-bucket-az-type' in resp and resp['x-cos-bucket-az-type'] == 'MAZ':
3749+
data.update({'MAZ': True})
3750+
else:
3751+
data.update({'MAZ': False})
3752+
data.update({"Location": resp['x-cos-bucket-region']})
3753+
url = self._conf.uri(bucket=Bucket).strip('/')
3754+
data.update({'BucketUrl': url})
3755+
pool.add_task(_head_bucket_wrapper, Bucket, **kwargs)
3756+
3757+
# Website
3758+
def _get_bucket_website_wrapper(Bucket, **kwargs):
3759+
try:
3760+
resp = self.get_bucket_website(Bucket, **kwargs)
3761+
data.update({'Website': resp})
3762+
except CosServiceError as e:
3763+
logger.debug("get_bucket_meta failed to get website conf:{}".format(e))
3764+
pool.add_task(_get_bucket_website_wrapper, Bucket, **kwargs)
3765+
3766+
# ObjectLock
3767+
def _get_bucket_object_lock_wrapper(Bucket, **kwargs):
3768+
try:
3769+
resp = self.get_bucket_object_lock(Bucket, **kwargs)
3770+
data.update({'ObjectLock': resp})
3771+
except CosServiceError as e:
3772+
logger.debug("get_bucket_meta failed to get object_lock conf:{}".format(e))
3773+
pool.add_task(_get_bucket_object_lock_wrapper, Bucket, **kwargs)
3774+
3775+
# ACL
3776+
def _get_bucket_acl_wrapper(Bucket, **kwargs):
3777+
try:
3778+
resp = self.get_bucket_acl(Bucket, **kwargs)
3779+
data.update({'ACL': resp})
3780+
except CosServiceError as e:
3781+
logger.debug("get_bucket_meta failed to get acl conf:{}".format(e))
3782+
pool.add_task(_get_bucket_acl_wrapper, Bucket, **kwargs)
3783+
3784+
# Logging
3785+
def _get_bucket_logging_wrapper(Bucket, **kwargs):
3786+
try:
3787+
resp = self.get_bucket_logging(Bucket, **kwargs)
3788+
data.update({'Logging': resp})
3789+
except CosServiceError as e:
3790+
logger.debug("get_bucket_meta failed to get logging conf:{}".format(e))
3791+
pool.add_task(_get_bucket_logging_wrapper, Bucket, **kwargs)
3792+
3793+
# Lifecycle
3794+
def _get_bucket_lifecycle_wrapper(Bucket, **kwargs):
3795+
try:
3796+
resp = self.get_bucket_lifecycle(Bucket, **kwargs)
3797+
data.update({'Lifecycle': resp})
3798+
except CosServiceError as e:
3799+
logger.debug("get_bucket_meta failed to get lifecycle conf:{}".format(e))
3800+
pool.add_task(_get_bucket_lifecycle_wrapper, Bucket, **kwargs)
3801+
3802+
# Replication
3803+
def _get_bucket_replication_wrapper(Bucket, **kwargs):
3804+
try:
3805+
resp = self.get_bucket_replication(Bucket, **kwargs)
3806+
data.update({'Replication': resp})
3807+
except CosServiceError as e:
3808+
logger.debug("get_bucket_meta failed to get replication conf:{}".format(e))
3809+
pool.add_task(_get_bucket_replication_wrapper, Bucket, **kwargs)
3810+
3811+
# Encryption
3812+
def _get_bucket_encryption_wrapper(Bucket, **kwargs):
3813+
try:
3814+
resp = self.get_bucket_encryption(Bucket, **kwargs)
3815+
data.update({'Encryption': resp})
3816+
except CosServiceError as e:
3817+
logger.debug("get_bucket_meta failed to get encryption conf:{}".format(e))
3818+
pool.add_task(_get_bucket_encryption_wrapper, Bucket, **kwargs)
3819+
3820+
# CORS
3821+
def _get_bucket_cors_wrapper(Bucket, **kwargs):
3822+
try:
3823+
resp = self.get_bucket_cors(Bucket, **kwargs)
3824+
data.update({'CORS': resp})
3825+
except CosServiceError as e:
3826+
logger.debug("get_bucket_meta failed to get cors conf:{}".format(e))
3827+
pool.add_task(_get_bucket_cors_wrapper, Bucket, **kwargs)
3828+
3829+
# Versioning
3830+
def _get_bucket_versioning_wrapper(Bucket, **kwargs):
3831+
try:
3832+
resp = self.get_bucket_versioning(Bucket, **kwargs)
3833+
data.update({'Versioning': resp})
3834+
except CosServiceError as e:
3835+
logger.debug("get_bucket_meta failed to get versioning conf:{}".format(e))
3836+
pool.add_task(_get_bucket_versioning_wrapper, Bucket, **kwargs)
3837+
3838+
# IntelligentTiering
3839+
def _list_bucket_intelligenttiering_conf_wrapper(Bucket, **kwargs):
3840+
try:
3841+
resp = self.list_bucket_intelligenttiering_configurations(Bucket, **kwargs)
3842+
data.update({'IntelligentTiering': resp})
3843+
except CosServiceError as e:
3844+
logger.debug("get_bucket_meta failed to get intelligenttiering conf:{}".format(e))
3845+
pool.add_task(_list_bucket_intelligenttiering_conf_wrapper, Bucket, **kwargs)
3846+
3847+
# Tagging
3848+
def _get_bucket_tagging_wrapper(Bucket, **kwargs):
3849+
try:
3850+
resp = self.get_bucket_tagging(Bucket, **kwargs)
3851+
data.update({'Tagging': resp})
3852+
except CosServiceError as e:
3853+
logger.debug("get_bucket_meta failed to get tagging conf:{}".format(e))
3854+
pool.add_task(_get_bucket_tagging_wrapper, Bucket, **kwargs)
3855+
3856+
pool.wait_completion()
3857+
return data
3858+
35393859
# service interface begin
35403860
def list_buckets(self, TagKey=None, TagValue=None, Region=None, CreateTime=None, Range=None, Marker="", MaxKeys=2000, **kwargs):
35413861
"""列出符合条件的bucket

0 commit comments

Comments
 (0)