The versioning state of a bucket applies to all objects in the bucket. If you enable versioning for a bucket, you can recover any previous version of an object in the bucket after accidentally overwriting or deleting it.
Notes
The sample code in this topic uses the region ID
cn-hangzhou
of the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket by using other Alibaba Cloud services in the same region in which the bucket is located, use an internal endpoint. For more information about the regions and endpoints supported by Object Storage Service (OSS), see OSS Regions and Endpoints.In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure Access Credentials.
The
oss:PutBucketVersioning
permission is required to configure versioning for a bucket. Theoss:GetBucketVersioning
permission is required to query the versioning state of a bucket. For more information, see Grant Custom Permissions to RAM Users.
Methods
Configure the versioning state of a bucket
func (c *Client) PutBucketVersioning(ctx context.Context, request *PutBucketVersioningRequest, optFns ...func(*Options)) (*PutBucketVersioningResult, error)
Query the versioning state of a bucket
func (c *Client) GetBucketVersioning(ctx context.Context, request *GetBucketVersioningRequest, optFns ...func(*Options)) (*GetBucketVersioningResult, error)
Request parameters
Parameters | Types | Description |
ctx | context.Context | The context of the request, which can be used to specify the total duration of the request. |
request | *PutBucketVersioningRequest | The request parameter to configure the versioning of the bucket. For more information, see PutBucketVersioningRequest. |
*GetBucketVersioningRequest | The request parameter to query the versioning state of a bucket. For more information, see GetBucketVersioningRequest. | |
optFns | ...func(*Options) | Optional. The operation-level parameters. For more information, see Options. |
Response parameters
Response parameters | Types | Description |
result | *PutBucketVersioningResult | The response to the operation. This parameter is available when the value of err is nil. For more information, see PutBucketVersioningResult. |
*GetBucketVersioningResult | The response to the operation. This parameter is available when the value of err is nil. For more information, see GetBucketVersioningResult. | |
err | error | The status of the request. If the request fails, the value of err is not nil. |
Sample code
References
For more information about the API operation that you can call to configure the versioning state of a bucket, see PutBucketVersioning.
For more information about the API operation that you can call to query the versioning state of a bucket, see GetBucketVersioning.