Lorsque vous téléchargez un objet de plus de 5 Go depuis Object Storage Service (OSS) vers votre ordinateur local, des interruptions réseau ou des plantages du programme peuvent entraîner l'échec du téléchargement. Si plusieurs tentatives échouent, utilisez le téléchargement avec reprise pour récupérer l'objet volumineux. Cette méthode divise l'objet en plusieurs parties et les télécharge en parallèle pour accélérer le processus. La progression du téléchargement est enregistrée dans un fichier de point de contrôle (checkpoint). En cas d'échec sur une partie, le téléchargement reprend à la position enregistrée. Une fois toutes les parties récupérées, elles sont assemblées pour former l'objet complet.
Notes d'utilisation
Cette rubrique utilise l'endpoint 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, privilégiez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Les identifiants d'accès sont récupérés depuis les variables d'environnement. Pour savoir comment les configurer, reportez-vous à la section Configurer les identifiants d'accès.
Nous illustrons ici la création d'une instance OSSClient via un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via Security Token Service (STS), consultez Configurer un client (Go SDK V1).
Le téléchargement avec reprise nécessite l'autorisation
oss:GetObject. Pour plus de détails, voir Accorder une politique personnalisée.Un fichier de point de contrôle enregistre la progression. Si une partie échoue, le téléchargement suivant reprend à la position sauvegardée. Ce fichier est supprimé automatiquement à la fin du processus.
OSS SDK for Go écrit la progression dans ce fichier de point de contrôle. Assurez-vous de disposer des permissions d'écriture nécessaires sur ce fichier.
Ne modifiez pas la somme de contrôle contenue dans le fichier de point de contrôle. Si ce fichier est corrompu, vous devez retélécharger toutes les parties.
Si la valeur ETag de l'objet est incohérente ou si des parties sont perdues ou modifiées pendant le téléchargement, relancez le téléchargement de l'objet entier.
Exemples
package main
import (
"fmt"
"os"
"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 the bucket. Example: examplebucket.
bucket, err := client.Bucket("examplebucket")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
// Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. If you do not specify the path of the local file, the downloaded object is saved to the path of the project to which the sample program belongs.
// Set the size of each part that you want to download to 100 KB (100 x 1024) and set the number of concurrent download parts to 3.
// oss.Checkpoint(true, "") Specify that resumable download is enabled. By default, the checkpoint file and the downloaded object are stored in the same directory and share the same name. However, the extension of the checkpoint file is .temp. You can also use oss.Checkpoint(true, "your-cp-file.temp") to specify the name of the checkpoint file.
err = bucket.DownloadFile("file.zip", "D:\\file.zip", 100*1024, oss.Routines(3), oss.Checkpoint(true, ""))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}