All Products
Search
Document Center

Object Storage Service:Manage versioning

Last Updated:Sep 29, 2024

The versioning setting of a bucket applies to all objects in the bucket. If you enable versioning for a bucket, you can restore a previous version of an object in the bucket when you accidentally overwrite or delete the object.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see 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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To configure versioning for a bucket, you must have the oss:PutBucketVersioning permission. To query the versioning state of a bucket, you must have the oss:GetBucketVersioning permission. For more information, see Attach a custom policy to a RAM user.

Sample code

Configure versioning for a bucket

The following code provides an example on how to enable versioning when you create a bucket:

package main

import (
    "log"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        log.Fatalf("Failed to create credentials provider: %v", err)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    endpoint := "yourEndpoint"
    client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        log.Fatalf("Failed to create OSS client: %v", err)
    }

    // Create a bucket. 
    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    err = client.CreateBucket(bucketName)
    if err != nil {
        log.Fatalf("Failed to create bucket '%s': %v", bucketName, err)
    }

    // Set the versioning status of the bucket to Enabled. 
    config := oss.VersioningConfig{Status: "Enabled"}
    err = client.SetBucketVersioning(bucketName, config)
    if err != nil {
        log.Fatalf("Failed to set bucket versioning for '%s': %v", bucketName, err)
    }

    log.Printf("Bucket '%s' created and versioning enabled successfully", bucketName)
}

Query the versioning status of a bucket

The following code provides an example on how to query the versioning status of a bucket:

package main

import (
    "log"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        log.Fatalf("Failed to create credentials provider: %v", err)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    endpoint := "yourEndpoint"
    client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        log.Fatalf("Failed to create OSS client: %v", err)
    }

    // Query the versioning status of the bucket. 
    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    ret, err := client.GetBucketVersioning(bucketName)
    if err != nil {
        log.Fatalf("Failed to get bucket versioning for '%s': %v", bucketName, err)
    }

    // Display the versioning status of the bucket. 
    log.Printf("The bucket Versioning status for '%s': %s", bucketName, ret.Status)
}

List the versions of all objects in a bucket

The following code provides an example on how to list the versions of all objects including delete markers in a specified bucket:

package main

import (
    "log"
    "os"

    "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
    provider, err := oss.NewEnvironmentVariableCredentialsProvider()
    if err != nil {
        log.Fatalf("Failed to create credentials provider: %v", err)
    }

    // Create an OSSClient instance. 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
    endpoint := "yourEndpoint"
    client, err := oss.New(endpoint, "", "", oss.SetCredentialsProvider(&provider))
    if err != nil {
        log.Fatalf("Failed to create OSS client: %v", err)
    }

    // Specify the name of the bucket. 
    bucketName := "yourBucketName"
    bucket, err := client.Bucket(bucketName)
    if err != nil {
        log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
    }

    // List the objects whose names contain the specified prefix. 
    prefix := "yourObjectPrefix"
    ret, err := bucket.ListObjectVersions(oss.Prefix(prefix))
    if err != nil {
        log.Fatalf("Failed to list object versions for bucket '%s' with prefix '%s': %v", bucketName, prefix, err)
    }

    // Display the information about the objects. 
    for _, object := range ret.ObjectVersions {
        log.Printf("Object: %v", object)
    }

    // Display the information about the delete markers. 
    for _, marker := range ret.ObjectDeleteMarkers {
        log.Printf("Delete Marker: %v", marker)
    }
}

References

  • For more information about the API operation that you can call to configure versioning for a bucket, see SetBucketVersioning.

  • For more information about the API operation that you can call to query the versioning status of a bucket, see GetBucketVersioning.

  • For more information about the API operation that you can call to list the versions of all objects in a bucket, see ListObjectVersions.