Tous les produits
Search
Centre de documentation

Object Storage Service:Télécharger un fichier (Go SDK V1)

Dernière mise à jour :Sep 23, 2026

Par défaut, l'opération GetObject renvoie uniquement la version actuelle d'un objet stocké dans un bucket pour lequel le contrôle de version est activé.

Contexte

Lorsque vous appelez l'opération GetObject sur un bucket, l'un des trois scénarios suivants se produit :

  • Si la version actuelle de l'objet est un marqueur de suppression, OSS renvoie le code d'erreur 404 Not Found.

  • Si vous spécifiez le paramètre versionId d'un objet dans la requête, la version correspondante est renvoyée. Si vous définissez versionId sur « null », la version de l'objet dont l'ID de version est nul est renvoyée.

  • Si vous tentez de récupérer un marqueur de suppression en spécifiant son versionId, OSS renvoie le code d'erreur 405 Method Not Allowed.

Notes

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

  • Dans cette rubrique, les identifiants d'accès sont récupérés à partir des variables d'environnement. Pour savoir comment configurer les identifiants d'accès, consultez Configurer les identifiants d'accès.

  • Cette rubrique illustre la création d'une instance OSSClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Configurer un client (Go SDK V1).

  • Pour télécharger un objet, vous devez disposer de l'autorisation oss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.

Exemple de code

Le code suivant montre comment télécharger un fichier.

package main

import (
	"log"
	"net/http"

	"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 Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, set the Endpoint as needed.
	// Set yourRegion to the region where the bucket is located. For example, for a bucket in the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, set the region 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"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	var retHeader http.Header
	// Download the file of the yourObjectVersionId version to the cache.
	// Set yourObjectName to the full path of the object. The full path does not include the bucket name.
	objectName := "yourObjectName"
	objectVersionId := "yourObjectVersionId"
	_, err = bucket.GetObject(objectName, oss.VersionId(objectVersionId), oss.GetResponseHeader(&retHeader))
	if err != nil {
		log.Fatalf("Failed to get object '%s' with version ID '%s': %v", objectName, objectVersionId, err)
	}

	// Print x-oss-version-id.
	versionId := oss.GetVersionId(retHeader)
	log.Printf("x-oss-version-id: %s", versionId)
}

Références

  • Pour obtenir l'exemple de code complet relatif au téléchargement d'un fichier, consultez l'exemple GitHub.

  • Pour plus d'informations sur l'opération API utilisée pour télécharger un fichier, consultez GetObject.