All Products
Search
Document Center

Object Storage Service:Determine whether a file exists (C# SDK V2)

Last Updated:Jun 04, 2026

Use IsObjectExistAsync in the OSS C# SDK V2 to check whether an object exists in a bucket.

Prerequisites

Before you begin, ensure that you have:

  • The sample code uses the China (Hangzhou) region (cn-hangzhou) and the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For region-to-endpoint mappings, see OSS regions and endpoints.

  • Access credentials configured as environment variables. For setup instructions, see [Configure access credentials for .NET 2.0].

  • Checking whether an object exists requires the oss:GetObject permission. For permission setup, see Grant a custom access policy to a RAM user.

Sample code

IsObjectExistAsync returns true if the object exists in the bucket, or false if it does not.

using OSS = AlibabaCloud.OSS.V2;

var region = "cn-hangzhou";  // Required. The region where the bucket is located.
var endpoint = null as string;  // Optional. The endpoint used to access OSS. Example: https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The name of the bucket.
var key = "your object name";  // Required. The name of the object. Format: folder/objectName.

// Load default SDK configurations. Credentials such as the AccessKey ID and secret are read from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Use environment variables (OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET) for authentication.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

// Create an OSS client.
using var client = new OSS.Client(cfg);

// Check whether the object exists.
var result = await client.IsObjectExistAsync(bucket, key);

Console.WriteLine("IsObjectExist done");
// true: the object exists. false: the object does not exist.
Console.WriteLine($"result: {result}");

References

For the complete sample code, see IsObjectExist.cs.