L'instabilité du réseau ou d'autres anomalies peuvent interrompre le téléchargement d'un objet volumineux. Parfois, le téléchargement échoue même après plusieurs tentatives. Pour résoudre ce problème, Object Storage Service (OSS) propose la fonctionnalité de téléchargement avec reprise. OSS divise l'objet en plusieurs parties et les télécharge séparément. Une fois toutes les parties récupérées, OSS les assemble pour reconstituer l'objet complet.
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.
Cette rubrique illustre la création d'une instance OSSClient à l'aide d'un point de terminaison OSS. Pour d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants Security Token Service (STS), reportez-vous à Créer une instance OssClient.
Pour utiliser le téléchargement avec reprise, vous devez disposer de l'autorisation
oss:GetObject. Pour plus d'informations, consultez Accorder une politique personnalisée.
Méthode d'implémentation
Utilisez la méthode OssClient.ResumableDownloadObject pour télécharger un objet avec reprise. La requête DownloadObjectRequest associée à cette méthode inclut les paramètres suivants :
|
Paramètre |
Description |
Obligatoire |
Valeur par défaut |
Configuration |
|
bucket |
Nom du bucket. |
Oui |
Aucune |
Défini dans le constructeur. |
|
key |
Chemin complet du fichier OSS. |
Oui |
Aucune |
Défini dans le constructeur. |
|
filePath |
Chemin complet du fichier local. |
Non |
Nom du fichier OSS |
Défini dans le constructeur. |
|
partSize |
Taille du shard. La valeur doit être comprise entre 100 Ko et 5 Go. |
Non |
8 Mo |
Défini via setPartSize. |
|
threadNum |
Nombre de shards téléchargés simultanément. |
Non |
3 |
Défini dans le constructeur ou via setThreadNum. |
|
checkpointDir |
Fichier enregistrant l'état du téléchargement des shards locaux. Définissez ce paramètre pour activer le téléchargement avec reprise. Les informations de progression sont sauvegardées dans ce fichier pendant le téléchargement. Si le téléchargement d'un shard échoue, la tentative suivante reprend à partir du point enregistré dans le fichier. Une fois le téléchargement terminé, ce fichier est supprimé. |
Non |
Même dossier que le fichier de téléchargement. |
Défini dans le constructeur ou via setCheckpointDir. |
Exemple de code
L'exemple de code suivant montre comment effectuer un téléchargement avec reprise :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the Region ID of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region ID to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Download the object to a local file named examplefile.txt and save it to the specified local path (D:\\localpath). If the local file exists, it is overwritten. If it does not exist, it is created. */
/* If you do not specify a local path, the downloaded file is saved to the local path of the project by default. */
std::string DownloadFilePath = "D:\\localpath\\examplefile.txt";
/* Set the folder for the breakpoint record file and make sure the specified folder exists, for example, D:\\localpath. */
/* If an object download is interrupted and a breakpoint record file is generated, you must set the corresponding breakpoint record file to resume the download. After the download is complete, this file is deleted. */
std::string CheckpointFilePath = "D:\\localpath";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this code sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Perform a resumable download. */
DownloadObjectRequest request(BucketName, ObjectName, DownloadFilePath, CheckpointFilePath);
auto outcome = client.ResumableDownloadObject(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "ResumableDownloadObject fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Références
Pour l'exemple de code complet relatif aux téléchargements avec reprise, consultez Exemple GitHub.
Pour plus d'informations sur l'opération API dédiée aux téléchargements avec reprise, consultez GetObject.