All Products
Search
Document Center

Object Storage Service:Check whether a file exists (Go SDK V1)

Last Updated:Mar 20, 2026

Use IsObjectExist to check if an object exists in a bucket. The method returns a boolean and an error, so you can distinguish between "object not found" and "request failed."

Prerequisites

Before you begin, make sure you have:

Usage notes

  • The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For more information, see Regions and endpoints.

  • To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Configure OSSClient instances.

Check if an object exists

IsObjectExist returns (bool, error).

Parameters

ParameterTypeDescription
objectNamestringFull path of the object, excluding the bucket name. For example, images/photo.jpg.

Return values

Return valueTypeDescription
isExistbooltrue if the object exists; false if it does not.
errerrorNon-nil if the request itself failed (for example, due to a network error or authentication failure). A false result with a nil error means the object does not exist.
package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables.
	// Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Replace yourEndpoint with the endpoint of your bucket region.
	// For China (Hangzhou), use https://oss-cn-hangzhou.aliyuncs.com.
	// Replace yourRegion with the region ID, for example, cn-hangzhou.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Get the bucket handle. Replace yourBucketName with the actual bucket name.
	bucket, err := client.Bucket("yourBucketName")
	if err != nil {
		log.Fatalf("Failed to get bucket: %v", err)
	}

	// Check if the object exists.
	// Replace yourObjectName with the full object path, excluding the bucket name.
	isExist, err := bucket.IsObjectExist("yourObjectName")
	if err != nil {
		// err is non-nil only when the request itself failed,
		// for example, due to a network error or invalid credentials.
		log.Fatalf("Failed to check if object exists: %v", err)
	}
	log.Printf("Exist: %t\n", isExist)
}

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
yourEndpointEndpoint of the bucket regionhttps://oss-cn-hangzhou.aliyuncs.com
yourRegionRegion ID of the bucketcn-hangzhou
yourBucketNameName of the bucketmy-bucket
yourObjectNameFull object path, excluding the bucket nameimages/photo.jpg

References