API | 操作名 | 操作描述 |
下载对象 | 下载一个对象至本地 |
// 高级下载接口支持断点续传,所以会在下载前先发起 HEAD 请求获取文件信息。// 如果您使用的是临时密钥或者使用子账号访问,请确保权限列表中包含 HeadObject 的权限。// 初始化 TransferConfig,这里使用默认配置,如果需要定制,请参考 SDK 接口文档TransferConfig transferConfig = new TransferConfig.Builder().build();//初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键//本地目录路径String savePathDir = context.getExternalCacheDir().toString();//本地保存的文件名,若不填(null),则与 COS 上的文件名一样String savedFileName = "exampleobject";Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext,bucket, cosPath, savePathDir, savedFileName);//设置下载进度回调cosxmlDownloadTask.setCosXmlProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long complete, long target) {// todo Do something to update progress...}});//设置返回结果回调cosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLDownloadTask.COSXMLDownloadTaskResult downloadTaskResult =(COSXMLDownloadTask.COSXMLDownloadTaskResult) result;}// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});//设置任务状态回调,可以查看任务过程cosxmlDownloadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
cosxmlDownloadTask.pause();
cosxmlDownloadTask.resume();
cosxmlDownloadTask.cancel();
// 初始化 TransferConfig,这里使用默认配置,如果需要定制,请参考 SDK 接口文档// TransferManager 支持断点下载,您只需要保证 bucket、cosPath、savePathDir、savedFileName// 参数一致,SDK 便会从上次已经下载的位置继续下载。TransferConfig transferConfig = new TransferConfig.Builder().build();//初始化 TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键//本地目录路径String savePathDir = context.getExternalCacheDir().toString();//本地保存的文件名,若不填(null),则与 COS 上的文件名一样String savedFileName = "exampleobject";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);
// 对象在存储桶中的位置标识符,即称对象键String[] cosPaths = new String[] {"exampleobject1","exampleobject2","exampleobject3",};for (String cosPath : cosPaths) {COSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext,bucket, cosPath, savePathDir, savedFileName);// 设置返回结果回调cosxmlDownloadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLDownloadTask.COSXMLDownloadTaskResult downloadResult =(COSXMLDownloadTask.COSXMLDownloadTaskResult) result;}// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});}
boolean isTruncated = true;String marker = null;try {while (isTruncated) {GetBucketRequest getBucketRequest = new GetBucketRequest(region, bucket, directoryPath);// 设置分页信息getBucketRequest.setMarker(marker);// 设置不查询子目录getBucketRequest.setDelimiter("/");GetBucketResult getBucketResult = cosXmlService.getBucket(getBucketRequest);// 批量下载for (ListBucket.Contents content : getBucketResult.listBucket.contentsList) {GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, content.key, savePathDir);transferManager.download(context,getObjectRequest);}isTruncated = getBucketResult.listBucket.isTruncated;marker = getBucketResult.listBucket.nextMarker;}} catch (CosXmlServiceException serviceException) {serviceException.printStackTrace();} catch (CosXmlClientException clientException) {clientException.printStackTrace();}
// 初始化 TransferConfig,这里使用默认配置,如果需要定制,请参考 SDK 接口文档TransferConfig transferConfig = new TransferConfig.Builder().build();// 初始化 TransferManagerCosXmlServiceConfig cosXmlServiceConfig = new CosXmlServiceConfig.Builder().setRegion("ap-guangzhou").builder();// 匿名下载生成 CosXmlService 不需要传入密钥生成器CosXmlService cosXmlService = new CosXmlService(context, cosXmlServiceConfig);TransferManager transferManager = new TransferManager(cosXmlService, transferConfig);// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键// 本地目录路径String savePathDir = context.getExternalCacheDir().toString();// 本地保存的文件名,若不填(null),则与 COS 上的文件名一样String savedFileName = "exampleobject";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);
String bucket = "examplebucket-1250000000"; //存储桶名称,格式:BucketName-APPIDString cosPath = "exampleobject"; //对象位于存储桶中的位置标识符,即对象键String savePath = context.getExternalCacheDir().toString(); //本地路径GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath,savePath);getObjectRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.getObjectAsync(getObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest,CosXmlResult cosXmlResult) {GetObjectResult getObjectResult = (GetObjectResult) cosXmlResult;}// 如果您使用 kotlin 语言来调用,请注意回调方法中的异常是可空的,否则不会回调 onFail 方法,即:// clientException 的类型为 CosXmlClientException?,serviceException 的类型为 CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
本页内容是否解决了您的问题?