IsObjectExist checks whether an object exists in a specified bucket and returns a boolean result.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, a public endpoint is used to access resources in a bucket. To access resources in the bucket from other Alibaba Cloud services in the same region, use an internal endpoint. For more information, see OSS regions and endpoints.Access credentials are obtained from environment variables. For more information, see Configure access credentials.
The
oss:GetObjectpermission 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. 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. Valid only when err is nil. |
| err | error | The status of the request. If the request fails, err is not nil. |
Sample code
The following example checks whether an object exists and handles three possible outcomes: the object exists, the object does not exist, and the request fails (for example, due to insufficient permissions or a network error).
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(®ion, "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, see GitHub example.
For API details, see IsObjectExist.