“S3”对象没有属性“get_object_lock_configuration”

问题描述

我正在尝试实现对象锁定功能,但函数(get/put_object_lock_configuration)不可用:

>>> import boto3
>>> boto3.__version__
'1.17.64'
>>> client = boto3.client('s3')
>>> client.get_object_lock_configuration
Traceback (most recent call last):
  File "<stdin>",line 1,in <module>
  File "/usr/lib/python3.6/site-packages/botocore/client.py",line 553,in __getattr__
    self.__class__.__name__,item)
AttributeError: 'S3' object has no attribute 'get_object_lock_configuration'

>>> client.get_object_lock_configuration(Bucket='tst',ExpectedBucketowner='tst')
Traceback (most recent call last):
  File "<stdin>",item)
AttributeError: 'S3' object has no attribute 'get_object_lock_configuration'

编辑: 对象锁定功能未在 python 中显示(选项卡选项卡):

>>> client.get_object_
client.get_object_acl(      client.get_object_tagging(  client.get_object_torrent(

>>> client.put_object
client.put_object(          client.put_object_acl(      client.put_object_tagging(

解决方法

get_object_lock_configuration 是 function 而非属性。

你需要这样称呼它:

response = client.get_object_lock_configuration(
    Bucket='string',ExpectedBucketOwner='string'
)
,

调用函数客户端的语法。get_object_lock_configuration

response = client.get_object_lock_configuration(
    Bucket='string',ExpectedBucketOwner='string'
)

调用函数客户端的语法。put_object_lock_configuration

response = client.put_object_lock_configuration(
    Bucket='string',ObjectLockConfiguration={
        'ObjectLockEnabled': 'Enabled','Rule': {
            'DefaultRetention': {
                'Mode': 'GOVERNANCE'|'COMPLIANCE','Days': 123,'Years': 123
            }
        }
    },RequestPayer='requester',Token='string',ContentMD5='string',ExpectedBucketOwner='string'
)

要了解更多信息,请参阅此:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.put_object_lock_configuration

编辑:

示例代码:

import json
import boto3

client = boto3.client('s3')

response = client.get_object_lock_configuration(
Bucket='anynewname')

print(response)

输出语法:

{
    'ObjectLockConfiguration': {
        'ObjectLockEnabled': 'Enabled','Years': 123
            }
        }
    }
}

注意:如果没有在bucket上设置对象锁配置,会报错。

{
  "errorMessage": "An error occurred (ObjectLockConfigurationNotFoundError) when calling the GetObjectLockConfiguration operation: Object Lock configuration does not exist for this bucket","errorType": "ClientError","stackTrace": [
    "  File \"/var/task/lambda_function.py\",line 7,in lambda_handler\n    response = client.get_object_lock_configuration(\n","  File \"/var/runtime/botocore/client.py\",line 357,in _api_call\n    return self._make_api_call(operation_name,kwargs)\n",line 676,in _make_api_call\n    raise error_class(parsed_response,operation_name)\n"
  ]
}