Tous les produits
Search
Centre de documentation

Object Storage Service:Téléchargement avec reprise (SDK OSS pour Java 1.0)

Dernière mise à jour :Aug 18, 2026

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.

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, 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, consultez Configuration des 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 avec des identifiants issus du Security Token Service (STS), consultez Configuration du client.

  • Pour utiliser le téléchargement avec reprise, vous devez disposer de l'autorisation oss:PutObject. Pour plus d'informations, consultez Accorder une politique personnalisée.

Exemples

L'exemple de code suivant montre comment effectuer un téléchargement avec reprise :

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;

public class UploadFile {
        public static void main(String[] args) throws Exception {
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 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.
        String region = "cn-hangzhou";
        // 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. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // Create an OSSClient instance. 
        // Call the shutdown method to release associated resources when the OSSClient is no longer in use.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();
        
        try {
            ObjectMetadata meta = new ObjectMetadata();
            // Specify the type of content that you want to upload. 
            // meta.setContentType("text/plain");

            // Specify the access control list (ACL) of the object that you want to upload. 
            // meta.setObjectAcl(CannedAccessControlList.Private);

            // Configure multiple parameters by using UploadFileRequest. 
            // Specify the name of the bucket such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. Do not include the bucket name in the full path. 
            UploadFileRequest uploadFileRequest = new UploadFileRequest("examplebucket","exampledir/exampleobject.txt");

            // Configure a single parameter by using UploadFileRequest.            
            // Specify the full path of the local file that you want to upload. Example: D:\\localpath\\examplefile.txt. By default, if you do not specify the full path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. 
            uploadFileRequest.setUploadFile("D:\\localpath\\examplefile.txt");
            // Specify the number of concurrent threads for the upload task. Default value: 1. 
            uploadFileRequest.setTaskNum(5);
            // Specify the part size. Unit: bytes. Valid values: 100 KB to 5 GB. Default value: 100 KB. 
            uploadFileRequest.setPartSize(1 * 1024 * 1024);
            // Specify whether to enable resumable upload. By default, resumable upload is disabled. 
            uploadFileRequest.setEnableCheckpoint(true);
            // Specify the checkpoint file that records the upload progress of each part. This checkpoint file stores information about the upload progress. If a part fails to be uploaded, the task can be continued based on the progress recorded in the checkpoint file. After the local file is uploaded to OSS, the checkpoint file is deleted. 
            // By default, if you do not specify this parameter, the checkpoint file is stored in the same directory as the local file that you want to upload. The directory is named ${uploadFile}.ucp. 
            uploadFileRequest.setCheckpointFile("yourCheckpointFile");
            // Configure object metadata. 
            uploadFileRequest.setObjectMetadata(meta);
            // Configure an upload callback. The parameter type is Callback. 
            //uploadFileRequest.setCallback("yourCallbackEvent");

            // Start resumable upload. 
            ossClient.uploadFile(uploadFileRequest);

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (Throwable ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
                // Shut down the OSSClient instance. 
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}   
        

Références

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