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:
The
oss:GetObjectpermission on the target object. For more information, see Attach a custom policy to a RAM userAccess credentials configured in environment variables. For more information, see Configure access credentials
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
| Parameter | Type | Description |
|---|---|---|
objectName | string | Full path of the object, excluding the bucket name. For example, images/photo.jpg. |
Return values
| Return value | Type | Description |
|---|---|---|
isExist | bool | true if the object exists; false if it does not. |
err | error | Non-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:
| Placeholder | Description | Example |
|---|---|---|
yourEndpoint | Endpoint of the bucket region | https://oss-cn-hangzhou.aliyuncs.com |
yourRegion | Region ID of the bucket | cn-hangzhou |
yourBucketName | Name of the bucket | my-bucket |
yourObjectName | Full object path, excluding the bucket name | images/photo.jpg |