Skip to content

Commit c160097

Browse files
author
libertyzhu
committed
get_bucket_meta接口
1 parent 4a3a73d commit c160097

File tree

2 files changed

+270
-2
lines changed

2 files changed

+270
-2
lines changed

qcloud_cos/cos_client.py

Lines changed: 220 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,7 +3468,7 @@ def put_bucket_intelligenttiering(self, Bucket, IntelligentTieringConfiguration=
34683468
"""设置存储桶智能分层配置
34693469
34703470
:param Bucket(string): 存储桶名称.
3471-
:param IntelligentTieringConfiguration(dict): 只能分层配置
3471+
:param IntelligentTieringConfiguration(dict): 智能分层配置
34723472
:param kwargs(dict): 设置请求headers.
34733473
:return: None.
34743474
@@ -3510,7 +3510,6 @@ def put_bucket_intelligenttiering(self, Bucket, IntelligentTieringConfiguration=
35103510
def get_bucket_intelligenttiering(self, Bucket, **kwargs):
35113511
"""获取存储桶智能分层配置
35123512
:param Bucket(string): 存储桶名称.
3513-
:param IntelligentTieringConfiguration(dict): 智能分层配置
35143513
:param kwargs(dict): 设置请求headers.
35153514
:return(dict): 智能分层配置.
35163515
@@ -3535,6 +3534,225 @@ def get_bucket_intelligenttiering(self, Bucket, **kwargs):
35353534
params=params)
35363535
data = xml_to_dict(rt.content)
35373536
return data
3537+
3538+
def put_bucket_object_lock(self, Bucket, ObjectLockConfiguration={}, **kwargs):
3539+
"""设置存储桶对象锁定配置
3540+
3541+
:param Bucket(string): 存储桶名称.
3542+
:param ObjectLockConfiguration(dict): 对象锁定配置.
3543+
:param kwargs(dict): 设置请求headers.
3544+
:return: None.
3545+
3546+
.. code-block:: python
3547+
3548+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3549+
client = CosS3Client(config)
3550+
3551+
object_lock_conf = {
3552+
'ObjectLockEnabled': 'Enabled',
3553+
}
3554+
client.put_bucket_object_lock(Bucket="bucket", ObjectLockConfiguration=objeck_lock_conf)
3555+
"""
3556+
3557+
xml_config = format_xml(data=ObjectLockConfiguration, root='ObjectLockConfiguration')
3558+
headers = mapped(kwargs)
3559+
headers['Content-MD5'] = get_md5(xml_config)
3560+
headers['Content-Type'] = 'application/xml'
3561+
params = {'object-lock': ''}
3562+
url = self._conf.uri(bucket=Bucket)
3563+
logger.info("put bucket object-lock, url=:{url} ,headers=:{headers}".format(
3564+
url=url,
3565+
headers=headers))
3566+
rt = self.send_request(
3567+
method='PUT',
3568+
url=url,
3569+
bucket=Bucket,
3570+
data=xml_config,
3571+
auth=CosS3Auth(self._conf, params=params),
3572+
headers=headers,
3573+
params=params)
3574+
return rt.headers
3575+
3576+
def get_bucket_object_lock(self, Bucket, **kwargs):
3577+
"""获取存储桶对象锁定配置
3578+
3579+
:param Bucket(string): 存储桶名称.
3580+
:param kwargs(dict): 设置请求headers.
3581+
:return(dict): 对象锁定配置.
3582+
3583+
.. code-block:: python
3584+
3585+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3586+
client = CosS3Client(config)
3587+
client.get_bucket_object_lock(Bucket="bucket")
3588+
"""
3589+
headers = mapped(kwargs)
3590+
params = {'object-lock': ''}
3591+
url = self._conf.uri(bucket=Bucket)
3592+
logger.info("get bucket object-lock, url=:{url} ,headers=:{headers}".format(
3593+
url=url,
3594+
headers=headers))
3595+
rt = self.send_request(
3596+
method='GET',
3597+
url=url,
3598+
bucket=Bucket,
3599+
auth=CosS3Auth(self._conf, params=params),
3600+
headers=headers,
3601+
params=params)
3602+
data = xml_to_dict(rt.content)
3603+
return data
3604+
3605+
def get_bucket_meta(self, Bucket, **kwargs):
3606+
"""获取存储桶各项配置
3607+
3608+
:param Bucket(string): 存储桶名称.
3609+
:param kwargs(dict): 设置请求headers.
3610+
:return(dict): 存储桶各项配置.
3611+
3612+
.. code-block:: python
3613+
3614+
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象
3615+
client = CosS3Client(config)
3616+
client.get_bucket_meta(Bucket="bucket")
3617+
"""
3618+
data = {
3619+
'BucketUrl': None,
3620+
'OFS': False,
3621+
'MAZ': False,
3622+
'Encryption': None,
3623+
'ACL': None,
3624+
'Website': None,
3625+
'Logging': None,
3626+
'CORS': None,
3627+
'Versioning': None,
3628+
'IntelligentTiering': None,
3629+
'Lifecycle': None,
3630+
'Tagging': None,
3631+
'ObjectLock': None,
3632+
'Replication': None,
3633+
}
3634+
pool = SimpleThreadPool(num_threads=10)
3635+
3636+
# HeadBucket
3637+
def _head_bucket_wrapper(Bucket, **kwargs):
3638+
resp = self.head_bucket(Bucket, **kwargs)
3639+
# x-cos-bucket-arch: 'OFS'
3640+
# x-cos-bucket-az-type: 'MAZ'
3641+
# x-cos-bucket-region: 'ap-beijing'
3642+
if 'x-cos-bucket-arch' in resp and resp['x-cos-bucket-arch'] == 'OFS':
3643+
data.update({'OFS': True})
3644+
else:
3645+
data.update({'OFS': False})
3646+
if 'x-cos-bucket-az-type' in resp and resp['x-cos-bucket-az-type'] == 'MAZ':
3647+
data.update({'MAZ': True})
3648+
else:
3649+
data.update({'MAZ': False})
3650+
data.update({"Location": resp['x-cos-bucket-region']})
3651+
url = self._conf.uri(bucket=Bucket)
3652+
data.update({'BucketUrl': url})
3653+
pool.add_task(_head_bucket_wrapper, Bucket, **kwargs)
3654+
3655+
# Website
3656+
def _get_bucket_website_wrapper(Bucket, **kwargs):
3657+
try:
3658+
resp = self.get_bucket_website(Bucket, **kwargs)
3659+
data.update({'Website': resp})
3660+
except CosServiceError as e:
3661+
logger.debug("get_bucket_meta failed to get website conf:{}".format(e))
3662+
pool.add_task(_get_bucket_website_wrapper, Bucket, **kwargs)
3663+
3664+
# ObjectLock
3665+
def _get_bucket_object_lock_wrapper(Bucket, **kwargs):
3666+
try:
3667+
resp = self.get_bucket_object_lock(Bucket, **kwargs)
3668+
data.update({'ObjectLock': resp})
3669+
except CosServiceError as e:
3670+
logger.debug("get_bucket_meta failed to get object_lock conf:{}".format(e))
3671+
pool.add_task(_get_bucket_object_lock_wrapper, Bucket, **kwargs)
3672+
3673+
# ACL
3674+
def _get_bucket_acl_wrapper(Bucket, **kwargs):
3675+
try:
3676+
resp = self.get_bucket_acl(Bucket, **kwargs)
3677+
data.update({'ACL': resp})
3678+
except CosServiceError as e:
3679+
logger.debug("get_bucket_meta failed to get acl conf:{}".format(e))
3680+
pool.add_task(_get_bucket_acl_wrapper, Bucket, **kwargs)
3681+
3682+
# Logging
3683+
def _get_bucket_logging_wrapper(Bucket, **kwargs):
3684+
try:
3685+
resp = self.get_bucket_logging(Bucket, **kwargs)
3686+
data.update({'Logging': resp})
3687+
except CosServiceError as e:
3688+
logger.debug("get_bucket_meta failed to get logging conf:{}".format(e))
3689+
pool.add_task(_get_bucket_logging_wrapper, Bucket, **kwargs)
3690+
3691+
# Lifecycle
3692+
def _get_bucket_lifecycle_wrapper(Bucket, **kwargs):
3693+
try:
3694+
resp = self.get_bucket_lifecycle(Bucket, **kwargs)
3695+
data.update({'Lifecycle': resp})
3696+
except CosServiceError as e:
3697+
logger.debug("get_bucket_meta failed to get lifecycle conf:{}".format(e))
3698+
pool.add_task(_get_bucket_lifecycle_wrapper, Bucket, **kwargs)
3699+
3700+
# Replication
3701+
def _get_bucket_replication_wrapper(Bucket, **kwargs):
3702+
try:
3703+
resp = self.get_bucket_replication(Bucket, **kwargs)
3704+
data.update({'Replication': resp})
3705+
except CosServiceError as e:
3706+
logger.debug("get_bucket_meta failed to get replication conf:{}".format(e))
3707+
pool.add_task(_get_bucket_replication_wrapper, Bucket, **kwargs)
3708+
3709+
# Replication
3710+
def _get_bucket_encryption_wrapper(Bucket, **kwargs):
3711+
try:
3712+
resp = self.get_bucket_encryption(Bucket, **kwargs)
3713+
data.update({'Encryption': resp})
3714+
except CosServiceError as e:
3715+
logger.debug("get_bucket_meta failed to get encryption conf:{}".format(e))
3716+
pool.add_task(_get_bucket_encryption_wrapper, Bucket, **kwargs)
3717+
3718+
# CORS
3719+
def _get_bucket_cors_wrapper(Bucket, **kwargs):
3720+
try:
3721+
resp = self.get_bucket_cors(Bucket, **kwargs)
3722+
data.update({'CORS': resp})
3723+
except CosServiceError as e:
3724+
logger.debug("get_bucket_meta failed to get cors conf:{}".format(e))
3725+
pool.add_task(_get_bucket_cors_wrapper, Bucket, **kwargs)
3726+
3727+
# Versioning
3728+
def _get_bucket_versioning_wrapper(Bucket, **kwargs):
3729+
try:
3730+
resp = self.get_bucket_versioning(Bucket, **kwargs)
3731+
data.update({'Versioning': resp})
3732+
except CosServiceError as e:
3733+
logger.debug("get_bucket_meta failed to get versioning conf:{}".format(e))
3734+
pool.add_task(_get_bucket_versioning_wrapper, Bucket, **kwargs)
3735+
3736+
# IntelligentTiering
3737+
def _get_bucket_intelligenttiering_wrapper(Bucket, **kwargs):
3738+
try:
3739+
resp = self.get_bucket_intelligenttiering(Bucket, **kwargs)
3740+
data.update({'IntelligentTiering': resp})
3741+
except CosServiceError as e:
3742+
logger.debug("get_bucket_meta failed to get intelligenttiering conf:{}".format(e))
3743+
pool.add_task(_get_bucket_intelligenttiering_wrapper, Bucket, **kwargs)
3744+
3745+
# Tagging
3746+
def _get_bucket_tagging_wrapper(Bucket, **kwargs):
3747+
try:
3748+
resp = self.get_bucket_tagging(Bucket, **kwargs)
3749+
data.update({'Tagging': resp})
3750+
except CosServiceError as e:
3751+
logger.debug("get_bucket_meta failed to get tagging conf:{}".format(e))
3752+
pool.add_task(_get_bucket_tagging_wrapper, Bucket, **kwargs)
3753+
3754+
pool.wait_completion()
3755+
return data
35383756

35393757
# service interface begin
35403758
def list_buckets(self, TagKey=None, TagValue=None, Region=None, CreateTime=None, Range=None, Marker="", MaxKeys=2000, **kwargs):

ut/test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
USE_CREDENTIAL_INST = os.environ["USE_CREDENTIAL_INST"]
3131
test_bucket = 'cos-python-v5-test-' + str(sys.version_info[0]) + '-' + str(
3232
sys.version_info[1]) + '-' + REGION + '-' + APPID
33+
test_worm_bucket = 'cos-python-v5-test-worm' + str(sys.version_info[0]) + '-' + str(
34+
sys.version_info[1]) + '-' + REGION + '-' + APPID
3335
copy_test_bucket = 'copy-' + test_bucket
3436
test_object = "test.txt"
3537
special_file_name = "中文" + \
@@ -761,6 +763,54 @@ def test_get_bucket_location():
761763
)
762764
assert response['LocationConstraint'] == REGION
763765

766+
def test_put_get_bucket_object_lock():
767+
"""bucket object_lock测试"""
768+
769+
# 创建worm测试桶
770+
try:
771+
response = client.create_bucket(Bucket=test_worm_bucket)
772+
except CosServiceError as e:
773+
error_code = e.get_error_code()
774+
if error_code == 'BucketAlreadyOwnedByYou' or error_code == 'BucketAlreadyExists':
775+
pass
776+
else:
777+
raise e
778+
779+
object_lock_conf = {
780+
'ObjectLockEnabled': 'Enabled',
781+
}
782+
response = client.put_bucket_object_lock(Bucket=test_worm_bucket, ObjectLockConfiguration=object_lock_conf)
783+
784+
time.sleep(3)
785+
response = client.get_bucket_object_lock(Bucket=test_worm_bucket)
786+
assert response
787+
assert response['ObjectLockEnabled'] == 'Enabled'
788+
789+
# test get_bucket_meta() by the way.
790+
meta = client.get_bucket_meta(Bucket=test_worm_bucket)
791+
assert meta
792+
793+
# 删除worm测试桶
794+
client.delete_bucket(Bucket=test_worm_bucket)
795+
796+
def test_get_bucket_meta():
797+
"""测试get_buckt_meta()接口"""
798+
response = client.get_bucket_meta(Bucket=test_bucket)
799+
assert response
800+
assert 'BucketUrl' in response
801+
assert 'OFS' in response
802+
assert 'MAZ' in response
803+
assert 'Encryption' in response
804+
assert 'ACL' in response
805+
assert 'Website' in response
806+
assert 'Logging' in response
807+
assert 'CORS' in response
808+
assert 'Versioning' in response
809+
assert 'IntelligentTiering' in response
810+
assert 'Lifecycle' in response
811+
assert 'Tagging' in response
812+
assert 'ObjectLock' in response
813+
assert 'Replication' in response
764814

765815
def test_get_service():
766816
"""列出账号下所有的bucket信息"""

0 commit comments

Comments
 (0)