By default, when you call the HeadObject operation on an object in a versioned bucket, only the metadata of the current version of the object is returned. To return the metadata of a specified version of an object, you can specify the version ID in request parameters.

Sample code

The following code provides an example on how to obtain the metadata of a specified version of an object:
package main

import (
  "fmt"
  "os"
  "github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
  // Create an OSSClient instance. 
  // Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify the actual region. 
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  // Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: exampleobject.txt. 
  objectName := "exampleobject.txt"

  // Specify the name of the bucket in which the object is stored. Example: examplebucket. 
  bucket, err := client.Bucket("examplebucket")
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  
  // Query part of metadata of the specified version of the object. 
  // Set youObjectVersionId to the version ID of the object. For more information about how to obtain the version ID of the object, see List the versions of all objects in a bucket. 
  props, err := bucket.GetObjectMeta(objectName, oss.VersionId("youObjectVersionId"))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  fmt.Println("Object Meta:", props)

  // Query all metadata of the specified version of the object. 
  // Set youObjectVersionId to the version ID of the object. 
  props, err = bucket.GetObjectDetailedMeta(objectName, oss.VersionId("youObjectVersionId"))
  if err != nil {
    fmt.Println("Error:", err)
    os.Exit(-1)
  }
  fmt.Println("Object Meta:", props)
}            

References

  • For more information about the complete sample code that is used to obtain object metadata, visit GitHub.
  • For more information about the API operation that is called to obtain object metadata, see GetObjectMeta.