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(); //本地文件的绝对路径PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);try {// 设置对象标签,标签集合中的 Key 和 Value 必须先进行 URL 编码putObjectRequest.setRequestHeaders("x-cos-tagging", "Key1=Value&Key2=Value2", false);} catch (CosXmlClientException e) {e.printStackTrace();}// 若存在初始化分块上传的 UploadId,则赋值对应的 uploadId 值用于续传;否则,赋值 nullString uploadId = null;// 上传文件COSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,srcPath, 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"; //对象在存储桶中的位置标识符,即称对象键PutObjectTaggingRequest putObjectTaggingRequest = new PutObjectTaggingRequest(bucket, cosPath);putObjectTaggingRequest.addTag("key", "value");try {PutObjectTaggingResult putObjectTaggingResult = cosXmlService.putObjectTagging(putObjectTaggingRequest);} catch (CosXmlClientException clientException) {clientException.printStackTrace();} catch (CosXmlServiceException serviceException) {serviceException.printStackTrace();}
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键GetObjectTaggingRequest getObjectTaggingRequest = new GetObjectTaggingRequest(bucket, cosPath);try {GetObjectTaggingResult getObjectTaggingResult = cosXmlService.getObjectTagging(getObjectTaggingRequest);} catch (CosXmlClientException clientException) {clientException.printStackTrace();} catch (CosXmlServiceException serviceException) {serviceException.printStackTrace();}
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; //对象在存储桶中的位置标识符,即称对象键DeleteObjectTaggingRequest deleteObjectTaggingRequest = new DeleteObjectTaggingRequest(bucket, cosPath);try {DeleteObjectTaggingResult deleteObjectTaggingResult = cosXmlService.deleteObjectTagging(deleteObjectTaggingRequest);} catch (CosXmlClientException clientException) {clientException.printStackTrace();} catch (CosXmlServiceException serviceException) {serviceException.printStackTrace();}
本页内容是否解决了您的问题?