All Products
Search
Document Center

Object Storage Service:Determine whether an object exists (Go SDK V2)

Last Updated:Aug 02, 2025

This topic describes how to use OSS SDK for Go to determine whether an object exists.

Usage notes

  • The sample code in this topic uses the region ID cn-hangzhou for the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about the regions and endpoints that OSS supports, 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.

  • The oss:GetObject permission is required to download an object to a local computer. For more information, see Grant custom permissions to RAM users.

Method definition

func (c *Client) IsObjectExist(ctx context.Context, bucket string, key string, optFns ...func(*IsObjectExistOptions)) (bool, error)

Request parameters

Parameter

Type

Description

ctx

context.Context

The context of the request. You can use this parameter to specify the total time limit for the request.

bucket

string

The name of the bucket.

key

string

The name of the object.

optFns

...func(*IsObjectExistOptions)

(Optional) The API-level configuration parameters. For more information, see IsObjectExistOptions.

Return values

Return value

Type

Description

flag

bool

The return value of the API operation. This parameter is valid only when err is nil.

err

error

The status of the request. If the request fails, err is not nil.

Sample code

You can use the following code to determine whether an object exists.

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 // The region.
	bucketName string // The name of the bucket.
	objectName string // The name of the object.
)

// The init function is 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 bucket name is empty.
	if len(bucketName) == 0 {
		flag.PrintDefaults()
		log.Fatalf("invalid parameters, bucket name required")
	}

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

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

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

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

	// Check whether the object exists.
	result, err := client.IsObjectExist(context.TODO(), bucketName, objectName)
	if err != nil {
		log.Fatalf("failed to check if object exists %v", err)
	}

	// Print the check result.
	log.Printf("is object exist: %#v\n", result)
}

References

  • For the complete sample code for determining whether an object exists, see GitHub example.

  • For more information about the API operation used to determine whether an object exists, see IsObjectExist.