Tous les produits
Search
Centre de documentation

Object Storage Service:Vérifier l'existence d'un fichier (Go SDK V1)

Dernière mise à jour :Sep 24, 2026

Cette rubrique explique comment vérifier si un fichier existe.

Notes

  • Cette rubrique utilise le point de terminaison public de la région Chine (Hangzhou). Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un point de terminaison interne. Pour en savoir plus sur les régions et les points de terminaison OSS, consultez Régions et points de terminaison.

  • Les identifiants d'accès sont récupérés depuis les variables d'environnement. Pour configurer vos identifiants d'accès, consultez Configurer les identifiants d'accès.

  • Le code présenté crée une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, comme l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configurer un client (Go SDK V1).

  • L'autorisation oss:GetObject est requise pour vérifier l'existence d'un objet. Pour plus d'informations, consultez Accorder une politique personnalisée.

Exemple de code

L'exemple de code suivant montre comment vérifier si un fichier existe.

package main

import (
	"log"

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

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// Create an OSSClient instance.
	// Set yourEndpoint to the Endpoint of the bucket. For example, for a bucket in the China (Hangzhou) region, set the value to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the value as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the value to cn-hangzhou. For other regions, set the value as needed.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Set the signature version.
	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)
	}

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

	// Check whether the file exists.
	// Set yourObjectName to the full path of the object. Do not include the bucket name.
	objectName := "yourObjectName" // Replace with the actual object path.
	isExist, err := bucket.IsObjectExist(objectName)
	if err != nil {
		log.Fatalf("Failed to check if object exists: %v", err)
	}
	log.Printf("Exist: %t\n", isExist)
}

Références

  • Pour plus d'informations sur l'opération API permettant de vérifier l'existence d'un fichier, consultez IsObjectExist.