API | 操作名 | 操作描述 |
简单上传对象 | 上传一个对象至存储桶 |
API | 操作名 | 操作描述 |
查询分块上传 | 查询正在进行中的分块上传信息 | |
初始化分块上传 | 初始化分块上传操作 | |
上传分块 | 分块上传对象 | |
查询已上传块 | 查询特定分块上传操作中的已上传的块 | |
完成分块上传 | 完成整个文件的分块上传 | |
终止分块上传 | 终止一个分块上传操作并删除已上传的块 |
// 初始化 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 srcPath = new File(context.getCacheDir(), "exampleobject").toString(); //本地文件的绝对路径//若存在初始化分块上传的 UploadId,则赋值对应的 uploadId 值用于续传;否则,赋值 nullString uploadId = null;// 上传文件COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,srcPath, uploadId);//设置初始化分块上传回调(5.9.7版本以及后续版本支持)cosxmlUploadTask.setInitMultipleUploadListener(new InitMultipleUploadListener() {@Overridepublic void onSuccess(InitiateMultipartUpload initiateMultipartUpload) {//用于下次续传上传的 uploadIdString uploadId = initiateMultipartUpload.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}});
TransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键// 上传字节数组byte[] bytes = "this is a test".getBytes(Charset.forName("UTF-8"));COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,bytes);//设置返回结果回调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();}}});
TransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键// 流式上传InputStream inputStream =new ByteArrayInputStream("this is a test".getBytes(Charset.forName("UTF-8")));COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,inputStream);//设置返回结果回调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();}}});
TransferConfig transferConfig = new TransferConfig.Builder().build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键// 文件的 UriUri uri = Uri.parse("exampleObject");// 若存在初始化分块上传的 UploadId,则赋值对应的 uploadId 值用于续传;否则,赋值 nullString uploadId = null;COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,uri, uploadId);//设置返回结果回调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();}}});
// 默认对大于或等于2M的文件自动进行分块上传,可以通过如下代码修改分块阈值TransferConfig transferConfig = new TransferConfig.Builder().setDivisionForUpload(2 * 1024 * 1024) // 设置大于等于 2M 的文件进行分块上传.build();TransferManager transferManager = new TransferManager(cosXmlService,transferConfig);
// 如果上传过程中,已经发起了最后的 Complete Multipart Upload 请求,那么暂停会失败boolean pauseSuccess = cosxmlUploadTask.pauseSafely();
// 如果暂停成功,可以恢复上传if (pauseSuccess) {cosxmlUploadTask.resume();}
cosxmlUploadTask.cancel();
//本地文件的绝对路径File[] files = new File(context.getCacheDir(), "exampleDirectory").listFiles();// 开始批量上传for (File file : files) {//若存在初始化分块上传的 UploadId,则赋值对应的 uploadId 值用于续传;否则,赋值 nullString uploadId = null;// 上传文件COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,file.getAbsolutePath(), uploadId);//设置返回结果回调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();}}});}
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";// 文件夹在存储桶中的位置标识符,即称对象键,必须以 '/' 结尾String cosPath = "exampleobject/";PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, new byte[0]);cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {PutObjectResult putObjectResult =(PutObjectResult) 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();}}});
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";// 文件夹在存储桶中的位置标识符,即称对象键,必须以 '/' 结尾String cosPath = "exampleobject/";String srcPath = new File(context.getCacheDir(), "exampleobject").toString(); //本地文件的绝对路径PutObjectRequest putObjectRequest= new PutObjectRequest(bucket, cosPath, srcPath);putObjectRequest.setPriorityLow(); // 设置为低优先级上传任务final COSXMLUploadTask uploadTask = transferManager.upload(putObjectRequest, null);uploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {}@Overridepublic void onFail(CosXmlRequest request, CosXmlClientException clientException, CosXmlServiceException serviceException) {}});
/
结尾,否则会被识别为文件夹。// 存储桶名称,由 bucketname-appid 组成,appid必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象位于存储桶中的位置标识符,即对象键。String srcPath = new File(context.getCacheDir(), "exampleobject").toString();//"本地文件的绝对路径";PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath,srcPath);putObjectRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {PutObjectResult putObjectResult = (PutObjectResult) result;}// 如果您使用 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();}}});
String bucket = "examplebucket-1250000000"; //存储桶名称,格式:BucketName-APPIDString cosPath = "exampleobject"; //对象位于存储桶中的位置标识符,即对象键。String srcPath = new File(context.getCacheDir(), "exampleobject").toString();//"本地文件的绝对路径";PostObjectRequest postObjectRequest = new PostObjectRequest(bucket, cosPath,srcPath);postObjectRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.postObjectAsync(postObjectRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {PutObjectResult putObjectResult = (PutObjectResult) result;}// 如果您使用 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();}}});
// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";ListMultiUploadsRequest listMultiUploadsRequest =new ListMultiUploadsRequest(bucket);cosXmlService.listMultiUploadsAsync(listMultiUploadsRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {ListMultiUploadsResult listMultiUploadsResult =(ListMultiUploadsResult) result;}// 如果您使用 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();}}});
// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即对象键。InitMultipartUploadRequest initMultipartUploadRequest =new InitMultipartUploadRequest(bucket, cosPath);cosXmlService.initMultipartUploadAsync(initMultipartUploadRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {// 分片上传的 uploadIduploadId = ((InitMultipartUploadResult) result).initMultipartUpload.uploadId;}// 如果您使用 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();}}});
// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即对象键UploadPartRequest uploadPartRequest = new UploadPartRequest(bucket, cosPath,partNumber, srcFile.getPath(), offset, PART_SIZE, uploadId);uploadPartRequest.setProgressListener(new CosXmlProgressListener() {@Overridepublic void onProgress(long progress, long max) {// todo Do something to update progress...}});cosXmlService.uploadPartAsync(uploadPartRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {String eTag = ((UploadPartResult) result).eTag;eTags.put(partNumber, eTag);}// 如果您使用 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();}}});
// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即对象键。ListPartsRequest listPartsRequest = new ListPartsRequest(bucket, cosPath,uploadId);cosXmlService.listPartsAsync(listPartsRequest, new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {ListParts listParts = ((ListPartsResult) result).listParts;}// 如果您使用 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();}}});
// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即对象键。CompleteMultiUploadRequest completeMultiUploadRequest =new CompleteMultiUploadRequest(bucket,cosPath, uploadId, eTags);cosXmlService.completeMultiUploadAsync(completeMultiUploadRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {CompleteMultiUploadResult completeMultiUploadResult =(CompleteMultiUploadResult) result;}// 如果您使用 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();}}});
// 存储桶名称,由 bucketname-appid 组成,appid 必须填入,可以在 COS 控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即对象键。AbortMultiUploadRequest abortMultiUploadRequest =new AbortMultiUploadRequest(bucket,cosPath, uploadId);cosXmlService.abortMultiUploadAsync(abortMultiUploadRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult result) {AbortMultiUploadResult abortMultiUploadResult =(AbortMultiUploadResult) result;}// 如果您使用 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();}}});
本页内容是否解决了您的问题?