Setting Object Lock

Last Updated At: 2025-10-21 09:10:00

Description

COS allows you to set object locking for existing buckets. The PUT Bucket ObjectLockConfiguration API is used to set the object locking feature for a bucket to meet compliance requirements.

Request

Request Example

PUT /?object-lock HTTP/1.1
Host: <BucketName-APPID>.<Endpoint>
Date: GMT Date
Authorization: Auth String 

Note:

  • <BucketName-APPID> is the bucket name with APPID as the suffix, for example, examplebucket-1250000000.
  • Authorization: Auth String (For details, refer to Request Signature).

Request Header

This API only uses common request headers. For details, see Common Request Headers documentation.

Request Body

<?xml version="1.0" encoding="UTF-8" ?>
<ObjectLockConfiguration>
       <ObjectLockEnabled>Enabled</ObjectLockEnabled> 
              <Rule> 
                    <DefaultRetention>
                         <Days>30</Days> 
                    </DefaultRetention> 
              </Rule> 
       </ObjectLockConfiguration> 

The detailed data is described as follows:

Node Name (Keyword) Parent Node Description Type Required
ObjectLockConfiguration No Object locking configuration Container Yes
ObjectLockEnabled ObjectLockConfiguration Whether object locking is enabled String Yes
Rule ObjectLockConfiguration Object locking rule Containers Yes
DefaultRetention ObjectLockConfiguration.Rule Default object locking period Containers Yes
Days ObjectLockConfiguration.Rule.DefaultRetention Duration of the default object locking period (1–36500) Int Yes

Note:

  • The ObjectLockEnabled parameter can only be set to Enabled. When it is set to Enabled, the object locking feature is enabled for the bucket.
  • Once the object locking feature is enabled, it cannot be disabled. Please proceed with caution.

Response

Response Headers

This API only returns common response headers. For details, please refer to the Public Response Headers document.

Response Body

The response body is empty.

Error Code

This API follows uniform error responses and error codes. For error codes other than the following error information, see Error Code.

HTTP Status Code Error Code Description
409 Conflict InvalidLockedTime If the value of Days is less than the original time, the following error is returned: The object locking time of the bucket cannot be less than the original time, and the value should be 1–36500 days.

Practical Case

Request

The following example sets object locking for the bucket examplebucket-1250000000, with a retention period of 1 day.

PUT /?object-lock= HTTP/1.1
Host: exmaplebucket-1250000000.cos.city.yfm4.fsphere.cn
Content-Length: 281
Content-Type: application/x-www-form-urlencoded
Authorization: Auth String

<ObjectLockConfiguration>
    <ObjectLockEnabled>Enabled</ObjectLockEnabled>
    <Rule>
        <DefaultRetention>
            <Days>1</Days>
        </DefaultRetention>
    </Rule>
</ObjectLockConfiguration>

Response

HTTP/1.1 200 OK
Content-Length: 0
Connection: keep-alive
Date: Fri, 09 Dec 2022 08:17:20 GMT
x-cos-request-id: NjM5MmVmMTBfNmM0ZTQ0MGJfMjA4****