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 if it is accidentally overwritten or deleted.
Notes
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see OSS regions and endpoints.This topic provides an example of how to obtain access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To set the versioning state of a bucket, you must have the
oss:PutBucketVersioningpermission. To obtain the versioning state of a bucket, you must have theoss:GetBucketVersioningpermission. For more information, see Grant custom access policies to RAM users.
Method definitions
Set the versioning state of a bucket
func (c *Client) PutBucketVersioning(ctx context.Context, request *PutBucketVersioningRequest, optFns ...func(*Options)) (*PutBucketVersioningResult, error)Obtain the versioning state of a bucket
func (c *Client) GetBucketVersioning(ctx context.Context, request *GetBucketVersioningRequest, optFns ...func(*Options)) (*GetBucketVersioningResult, error)Request parameters
Parameter | Type | Description |
ctx | context.Context | The context of a request. You can use this parameter to set the total timeout period of a request. |
request | *PutBucketVersioningRequest | The request parameters to set the versioning state of a bucket. For more information, see PutBucketVersioningRequest. |
*GetBucketVersioningRequest | The request parameters to obtain the versioning state of a bucket. For more information, see GetBucketVersioningRequest. | |
optFns | ...func(*Options) | (Optional) The operation-level configuration parameters. For more information, see Options. |
Return values
Return value | Type | Description |
result | *PutBucketVersioningResult | The return value of the operation to set the versioning state of a bucket. This parameter is valid when err is nil. For more information, see PutBucketVersioningResult. |
*GetBucketVersioningResult | The return value of the operation to obtain the versioning state of a bucket. This parameter is valid when err is nil. For more information, see GetBucketVersioningResult. | |
err | error | The status of the request. If the request fails, err is not nil. |
Sample code
References
For more information about the API operation to set the versioning state of a bucket, see PutBucketVersioning.
For more information about the API operation to obtain the versioning state of a bucket, see GetBucketVersioning.