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 srcPath = new File(context.getCacheDir(), "exampleobject").toString(); //本地文件的绝对路径//若存在初始化分块上传的 UploadId,则赋值对应的 uploadId 值用于续传;否则,赋值 nullString uploadId = null;PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);// 设置单链接限速,单位为 bit/s,示例设置为 1M/sputObjectRequest.setTrafficLimit(1024 * 1024 * 8);// 上传文件COSXMLUploadTask cosxmlUploadTask = transferManager.upload(putObjectRequest, uploadId);//设置上传进度回调cosxmlUploadTask.setCosXmlProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long complete, long target) {// todo Do something to update progress...}});//设置返回结果回调cosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =(COSXMLUploadTask.COSXMLUploadTaskResult) 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();}}});//设置任务状态回调, 可以查看任务过程cosxmlUploadTask.setTransferStateListener(new TransferStateListener() {@Overridepublic void onStateChanged(TransferState state) {// todo notify transfer state}});
//.cssg-snippet-body-start:[transfer-download-object]// 高级下载接口支持断点续传,所以会在下载前先发起 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";GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, cosPath, savePathDir, savedFileName);// 设置单链接限速,单位为 bit/s,示例设置为 1M/sgetObjectRequest.setTrafficLimit(1024 * 1024 * 8);Context applicationContext = context.getApplicationContext(); // application// contextCOSXMLDownloadTask cosxmlDownloadTask =transferManager.download(applicationContext, getObjectRequest);//设置下载进度回调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}});
本页内容是否解决了您的问题?