Versioning applies to all objects in a bucket.
By versioning, you can restore an object in the bucket to any of its historical versions if it is accidentally overwritten or deleted. A bucket can be in the following three versioning states: not versioned (default), enabled, and suspended.
For more information about versioning, see Introduction to versioning in the Developer Guide.
Set the versioning state of a bucket
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Creates an OSSClient instance.
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Creates a bucket.
err = client.CreateBucket("<yourBucketName>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Sets the versioning state of the bucket to Enabled or Suspended. In this example, the versioning states is set to Enabled.
config := oss.VersioningConfig{Status: "Enabled"}
err = client.SetBucketVersioning("<yourBucketName>", config)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
For more information about setting the versioning state of a bucket, see PutBucketVersioning.
Obtain the versioning state of a bucket
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Creates an OSSClient instance.
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtains the versioning state of the bucket.
ret, err := client.GetBucketVersioning("<yourBucketName>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Prints the versioning state of the bucket.
fmt.Println("The bucket Versioning status:", ret.Status)
}
For more information about obtaining the versioning state of a bucket, see GetBucketVersioning.
List the versions of all objects in a bucket
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Creates an OSSClient instance.
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Obtains the bucket.
bucket, err := client.Bucket("<yourBucketName>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Lists objects with the specified prefix.
ret, err := bucket.ListObjectVersions(oss.Prefix("yourObjectPrefix"))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Prints the object information.
for _, object := range ret.ObjectVersions {
fmt.Println("Object:", object)
}
// Prints the delete markers.
for _, marker := range ret.ObjectDeleteMarkers {
fmt.Println("Delete Markers:", marker)
}
}
For more information about listing the versions of all objects (including delete markers) in a bucket, see GetBucketVersions (ListObjectVersions).