Java断点续传上传

通过断点续传上传的方式将文件上传到OSS前,您可以指定断点记录点。上传过程中,如果出现网络异常或程序崩溃导致文件上传失败时,将从断点记录处继续上传未上传完成的部分。

注意事项

  • 本文以华东1(杭州)外网Endpoint为例。如果您希望通过与OSS同地域的其他阿里云产品访问OSS,请使用内网Endpoint。关于OSS支持的RegionEndpoint的对应关系,请参见OSS地域和访问域名

  • 本文以从环境变量读取访问凭证为例。如何配置访问凭证,请参见Java配置访问凭证

  • 本文以OSS域名新建OSSClient为例。如果您希望通过自定义域名、STS等方式新建OSSClient,请参见常见场景配置示例

  • 要断点续传上传,您必须有oss:PutObject权限。具体操作,请参见RAM用户授予自定义的权限策略

示例代码

以下代码用于断点续传上传。

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 {
            // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // 填写Bucket所在地域。以华东1(杭州)为例,Region填写为cn-hangzhou。
            String region = "cn-hangzhou";
            // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

            // 创建OSSClient实例。
            // 当OSSClient实例不再使用时,调用shutdown方法以释放资源。
            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();
                // 指定上传的内容类型。
                // meta.setContentType("text/plain");

                // 文件上传时设置访问权限ACL。
                // meta.setObjectAcl(CannedAccessControlList.Private);

                // 通过UploadFileRequest设置多个参数。
                // 依次填写Bucket名称(例如examplebucket)以及Object完整路径(例如exampledir/exampleobject.txt),Object完整路径中不能包含Bucket名称。
                UploadFileRequest uploadFileRequest = new UploadFileRequest("examplebucket","exampledir/exampleobject.txt");

                // 通过UploadFileRequest设置单个参数。
                // 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
                uploadFileRequest.setUploadFile("D:\\localpath\\examplefile.txt");
                // 指定上传并发线程数,默认值为1。
                uploadFileRequest.setTaskNum(5);
                // 指定上传的分片大小,单位为字节,取值范围为100 KB~5 GB。默认值为100 KB。
                uploadFileRequest.setPartSize(1 * 1024 * 1024);
                // 开启断点续传,默认关闭。
                uploadFileRequest.setEnableCheckpoint(true);
                // 记录本地分片上传结果的文件。上传过程中的进度信息会保存在该文件中,如果某一分片上传失败,再次上传时会根据文件中记录的点继续上传。上传完成后,该文件会被删除。
                // 如果未设置该值,默认与待上传的本地文件同路径,名称为${uploadFile}.ucp。
                uploadFileRequest.setCheckpointFile("yourCheckpointFile");
                // 文件的元数据。
                uploadFileRequest.setObjectMetadata(meta);
                // 设置上传回调,参数为Callback类型。
                //uploadFileRequest.setCallback("yourCallbackEvent");

                // 断点续传上传。
                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 {
                // 关闭OSSClient。
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }
}

相关文档

关于断点续传上传的完整示例代码,请参见GitHub示例