Tous les produits
Search
Centre de documentation

Object Storage Service:Conditional download (OSS SDK for Go 1.0)

Dernière mise à jour :Sep 22, 2026

Lorsque vous téléchargez un seul objet depuis un bucket, vous pouvez spécifier des conditions de téléchargement basées sur la date de dernière modification ou sur l'ETag (l'identifiant du contenu de l'objet). Si les conditions spécifiées sont remplies, l'objet est téléchargé. Dans le cas contraire, une erreur est renvoyée et l'objet n'est pas téléchargé. Le téléchargement conditionnel permet de réduire la transmission réseau et la consommation de ressources, tout en améliorant l'efficacité des téléchargements.

Notes

  • Cette rubrique utilise l'endpoint 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 qu'OSS, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.

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

  • Cette rubrique montre comment créer 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).

  • Le téléchargement conditionnel nécessite l'autorisation oss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.

Conditions

Le tableau suivant décrit les conditions de téléchargement d'objets disponibles.

Remarque
  • Les paramètres If-Modified-Since et If-Unmodified-Since peuvent être utilisés conjointement. Il en va de même pour If-Match et If-None-Match.

  • Obtenez l'ETag en appelant ossClient.getObjectMetadata.

Paramètre

Description

Méthode de configuration

If-Modified-Since

Si l'heure spécifiée est antérieure à la date de dernière modification de l'objet, celui-ci peut être téléchargé. Sinon, le code 304 Not Modified est renvoyé.

oss.IfModifiedSince

If-Unmodified-Since

Si l'heure spécifiée est postérieure ou égale à la date de dernière modification de l'objet, celui-ci peut être téléchargé. Sinon, le code 412 Precondition Failed est renvoyé.

oss.IfUnmodifiedSince

If-Match

Si l'ETag spécifié correspond à celui de l'objet, ce dernier peut être téléchargé. Sinon, le code 412 Precondition Failed est renvoyé.

oss.IfMatch

If-None-Match

Si l'ETag spécifié ne correspond pas à celui de l'objet, ce dernier peut être téléchargé. Sinon, le code 304 Not Modified est renvoyé.

oss.IfNoneMatch

Exemples

L'exemple de code suivant illustre comment effectuer un téléchargement conditionnel :

package main

import (
	"fmt"
	"os"
	"time"

	"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 configured. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Create an OSSClient instance. 
        // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// Specify the name of your bucket.
	bucket, err := client.Bucket("yourBucketName")
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// For example, an object was last modified at 18:43:02, on November 21, 2023. If the specified time is earlier than the last modified time, the object meets the If-Modified-Since condition and the object is downloaded. 
	date := time.Date(2023, time.November, 21, 10, 40, 02, 0, time.UTC)

	// The object is not downloaded if it does not meet the specified conditions. 
        // Specify the full path of the object. Do not include the bucket name in the full path. 
	err = bucket.GetObjectToFile("yourObjectName", "LocalFile", oss.IfUnmodifiedSince(date))
	if err == nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	// The object is downloaded if it meets the specified conditions. 
	err = bucket.GetObjectToFile("yourObjectName", "LocalFile", oss.IfModifiedSince(date))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
}

Références

  • Pour consulter l'exemple de code complet utilisé pour effectuer un téléchargement conditionnel, rendez-vous sur GitHub.

  • Pour plus d'informations sur l'opération API permettant d'effectuer un téléchargement conditionnel, consultez GetObject.