腾讯云对象存储TransferManager

TransferManager概述

你可以使用AWSSDKforJavaTransferManager类进行上传、下载以及获取传输进度。更多详细说明请参考AWSSDKforJava/TransferManager。

初始化TransferManager

使用TransferManager传输前,需要初始化TransferManager腾讯云服务器,示例如下:

importcom.amazonaws.auth.AWSCredentialsProvider;importcom.amazonaws.auth.AWSStaticCredentialsProvider;importcom.amazonaws.client.builder.AwsClientBuilder;importcom.amazonaws.services.s3.AmazonS3;importcom.amazonaws.services.s3.AmazonS3Client;importcom.amazonaws.auth.AWSCredentials;importcom.amazonaws.auth.BasicAWSCredentials;importcom.amazonaws.ClientConfiguration;importcom.amazonaws.Protocol;importcom.amazonaws.SDKGlobalConfiguration;importcom.amazonaws.services.s3.transfer.TransferManager;importcom.amazonaws.services.s3.transfer.TransferManagerBuilder;publicclassS3SdkTest{publicstaticvoidmain(String[]args){finalStringaccessKey="youraccesskey";finalStringsecretKey="yoursecretkey";finalStringendpoint="https://s3.REGION.jdcloud-oss.com";ClientConfigurationconfig=newClientConfiguration();AwsClientBuilder.EndpointConfigurationendpointConfig=newAwsClientBuilder.EndpointConfiguration(endpoint,"REGION");AWSCredentialsawsCredentials=newBasicAWSCredentials(accessKey,secretKey);AWSCredentialsProviderawsCredentialsProvider=newAWSStaticCredentialsProvider(awsCredentials);//初始化s3腾讯云服务器AmazonS3s3=AmazonS3Client.builder().withEndpointConfiguration(endpointConfig).withClientConfiguration(config).withCredentials(awsCredentialsProvider).disableChunkedEncoding().withPathStyleAccessEnabled(true).build();//初始化TransferManager腾讯云服务器TransferManagerxfer_mgr=TransferManagerBuilder.standard().withS3Client(s3).build();//使用xfer_mgr进行上传下载......}}上传

TransferManager可将单个文件或整个目录上传到存储桶中。

上传单个文件Stringbucket_name="your-bucket";Stringkey_name="your-key";Stringfile_path="your-path";Filef=newFile(file_path);try{//上传文件(支持5G以上文件)Uploadxfer=xfer_mgr.upload(bucket_name,key_name,f);//检查传输状态XferMgrProgress.showTransferProgress(xfer);//等待传输完成XferMgrProgress.waitForCompletion(xfer);}catch(AmazonServiceExceptione){System.err.println(e.getErrorMessage());System.exit(1);}上传目录Stringbucket_name="your-bucket";Stringkey_prefix="your-prefix";Stringdir_path="your-path";try{//上传dir_path目录下所有文件(包含子目录)MultipleFileUploadxfer=xfer_mgr.uploadDirectory(bucket_name,key_prefix,newFile(dir_path),true);//检查传输状态XferMgrProgress.showMultiUploadProgress(xfer);//等待传输完成XferMgrProgress.waitForCompletion(xfer);}catch(AmazonServiceExceptione){System.err.println(e.getErrorMessage());System.exit(1);}下载

TransferManager可将单个对象或整个目录下载到本地。

下载单个对象Stringbucket_name="your-bucket";Stringkey_name="your-key";Stringfile_path="your-path";Filef=newFile(file_path);try{Downloadxfer=xfer_mgr.download(bucket_name,key_name,f);//检查传输状态XferMgrProgress.showTransferProgress(xfer);//等待传输完成XferMgrProgress.waitForCompletion(xfer);}catch(AmazonServiceExceptione){System.err.println(e.getErrorMessage());System.exit(1);}下载目录Stringbucket_name="your-bucket";Stringkey_prefix="your-prefix";Stringdir_path="your-path";try{MultipleFileDownloadxfer=xfer_mgr.downloadDirectory(bucket_name,key_prefix,newFile(dir_path));//检查传输状态XferMgrProgress.showTransferProgress(xfer);//等待传输完成XferMgrProgress.waitForCompletion(xfer);}catch(AmazonServiceExceptione){System.err.println(e.getErrorMessage());System.exit(1);}获取传输进度

XferMgrProgress类提供waitForCompletion、showTransferProgress、showMultiUploadProgress等静态方法。具体示例参考:XferMgrProgress.java