All Products
Search
Document Center

Object Storage Service:Query the metadata of an object using OSS SDK for G0 2.0

Last Updated:Nov 20, 2025

This topic describes how to query the metadata of objects in a versioning-enabled bucket.

Usage 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 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.

  • To query the metadata of objects, you must have the oss:GetObject permission. For more information, see Grant custom policy to RAM users.

Sample code

Call the HeadObject operation to query all metadata of an object

The following sample code provides an example of how to query all metadata of an object of the specified version by calling the HeadObject operation.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // Region in which your bucket is located.
	bucketName string // Name of the bucket.
	objectName string // Name of the object.
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check whether the name of the object is specified.
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a HeadObject request.
	request := &oss.HeadObjectRequest{
		Bucket:    oss.Ptr(bucketName),      // Name of the bucket.
		Key:       oss.Ptr(objectName),      // Name of the object.
		VersionId: oss.Ptr("yourVersionId"), // Version ID of the object.
	}

	// Perform the HeadObject operation and process the result.
	result, err := client.HeadObject(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to head object %v", err)
	}

	// Display the result.
	log.Printf("head object result:%#v\n", result)
}

Call the GetObjectMeta operation to query part of the metadata of an object

Note

Only part of the the metadata of an object can be queried by calling the GetObjectMeta operation, including the length of the returned content (ContentLength), ETag, the last modified time of the returned object (LastModified), the last accessed time of the object (LastAccessTime), the version ID of the object (VersionId), and the 64-bit CRC value (HashCRC64) of the object.

The following sample code provides an example of how to query part of the metadata of an object by calling the GetObjectMeta operation.

package main

import (
	"context"
	"flag"
	"log"

	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)

// Define global variables.
var (
	region     string // Region in which your bucket is located.
	bucketName string // Name of the bucket.
	objectName string // Name of the object
)

// Specify the init function used to initialize command line parameters.
func init() {
	flag.StringVar(&region, "region", "", "The region in which the bucket is located.")
	flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
	flag.StringVar(&objectName, "object", "", "The name of the object.")
}

func main() {
	// Parse command line parameters.
	flag.Parse()

	// Check whether the name of the bucket is specified.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

	// Check whether the region is specified.
	if len(region) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, region required")
	}

	// Check whether the name of the object is specified.
	if len(objectName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, object name required")
	}

	// Load the default configurations and specify the credential provider and region.
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(region)

	// Create an OSS client.
	client := oss.NewClient(cfg)

	// Create a request to query the metadata of an object.
	request := &oss.GetObjectMetaRequest{
		Bucket:    oss.Ptr(bucketName),      // Name of the bucket.
		Key:       oss.Ptr(objectName),      // Name of the object
		VersionId: oss.Ptr("yourVersionId"), // Version ID of the object.
	}

	// Perform the query operation and process the result.
	result, err := client.GetObjectMeta(context.TODO(), request)
	if err != nil {
		log.Fatalf("failed to get object meta %v", err)
	}

	// Display the result.
	log.Printf("get object meta result:%#v\n", result)
}

References

  • For the complete sample code that is used to query the metadata of an object, visit GitHub sample.

  • For more information about the API operation that you can call to query the metadata of an object, see HeadObject and GetObjectMeta.