Lorsque vous téléchargez un objet vers Object Storage Service (OSS) via un téléchargement avec reprise, vous pouvez spécifier un répertoire pour le fichier de point de contrôle qui enregistre la progression du téléchargement. Si le téléchargement d'un objet échoue en raison d'une exception réseau ou d'une erreur de programme, la tâche de téléchargement reprend à partir de la position enregistrée dans le fichier de point de contrôle.
Précautions
Dans cette rubrique, l'endpoint public de la région Chine (Hangzhou) est utilisé. Pour accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus de détails sur les régions et endpoints pris en charge, consultez Régions et endpoints.
Dans cette rubrique, les identifiants d'accès sont obtenus à partir des variables d'environnement. Pour plus d'informations sur la configuration des identifiants d'accès, consultez Configurer les identifiants d'accès à l'aide du SDK OSS pour Python 1.0.
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 avec des identifiants issus du Security Token Service (STS), consultez Initialisation.
Le téléchargement avec reprise nécessite les permissions
oss:PutObjectet oss:ListParts. Pour plus d'informations, consultez Accorder des politiques d'accès personnalisées aux utilisateurs RAM.Le téléchargement avec reprise est multithread. N'utilisez pas de multithreading externe lors de l'appel de cette fonction. Sinon, les données risquent d'être transmises plusieurs fois.
Vous pouvez augmenter la taille des parties si les conditions réseau sont bonnes et la diminuer si elles sont mauvaises.
Exemple de code
L'exemple de code suivant montre comment effectuer un téléchargement avec reprise :
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Set yourObjectName to the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt.
# Set yourLocalFile to the full path of the local file. Example: D:\\localpath\\examplefile.txt. If you do not specify a local path, the file is uploaded from the local path that corresponds to the project to which the sample program belongs.
oss2.resumable_upload(bucket, 'exampledir/exampleobject.txt', 'D:\\localpath\\examplefile.txt')
# If you do not use the store parameter to specify a directory, the .py-oss-upload directory is created in the HOME directory to save breakpoint information.
# Python SDK 2.1.0 and later support the following optional parameters for resumable upload.
# import sys
# # If the length of the data to be uploaded cannot be determined, the value of total_bytes is None.
# def percentage(consumed_bytes, total_bytes):
# if total_bytes:
# rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
# print('\r{0}% '.format(rate), end='')
# sys.stdout.flush()
# # If you use the store parameter to specify a directory, breakpoint information is saved in the specified directory. If you use num_threads to set the number of concurrent upload threads, set oss2.defaults.connection_pool_size to a value that is greater than or equal to the number of concurrent upload threads. The default number of concurrent upload threads is 1.
# oss2.resumable_upload(bucket, '<yourObjectName>', '<yourLocalFile>',
# store=oss2.ResumableStore(root='/tmp'),
# # Specify that multipart upload is used when the file size is greater than or equal to the value of the optional parameter multipart_threshold. The default value is 10 MB.
# multipart_threshold=100*1024,
# # Set the part size in bytes. The value must be in the range of 100 KB to 5 GB. The default value is 100 KB.
# part_size=100*1024,
# # Set the upload progress callback function.
# progress_callback=percentage,
# # If you use num_threads to set the number of concurrent upload threads, set oss2.defaults.connection_pool_size to a value that is greater than or equal to the number of concurrent upload threads. The default number of concurrent upload threads is 1.
# num_threads=4)
Références
Pour obtenir l'exemple de code complet relatif au téléchargement avec reprise, consultez l'exemple GitHub.