API | Operation | Description |
Tagging an object | Tags an uploaded object. | |
Querying object tags | Queries all tags of an object. | |
Deleting object tags | Deletes all tags of an object. |
x-cos-tagging
to Key1=Value1&Key2=Value2
. The tag keys and tag values in the set must be URL-encoded.// Initialize TransferConfig. The default configuration is used here. To customize the configuration, please see the SDK API documentation.TransferConfig transferConfig = new TransferConfig.Builder().build();// Initialize TransferManagerTransferManager transferManager = new TransferManager(cosXmlService,transferConfig);// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object keyString srcPath = new File(context.getCacheDir(), "exampleobject").toString(); // Absolute path of the local filePutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);try {// Set object tags. The tag keys and tag values in the set must be URL-encodedputObjectRequest.setRequestHeaders("x-cos-tagging", "Key1=Value&Key2=Value2", false);} catch (CosXmlClientException e) {e.printStackTrace();}// If there is an `uploadId` for an initialized multipart upload, assign the value of the `uploadId` here to resume the upload; otherwise, assign `null`String uploadId = null;// Upload the objectCOSXMLUploadTask cosxmlUploadTask = transferManager.upload(bucket, cosPath,srcPath, uploadId);// Set the response callbackcosxmlUploadTask.setCosXmlResultListener(new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {COSXMLUploadTask.COSXMLUploadTaskResult uploadResult =(COSXMLUploadTask.COSXMLUploadTaskResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest request,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object keyPutObjectTaggingRequest 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();}
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object keyGetObjectTaggingRequest getObjectTaggingRequest = new GetObjectTaggingRequest(bucket, cosPath);try {GetObjectTaggingResult getObjectTaggingResult = cosXmlService.getObjectTagging(getObjectTaggingRequest);} catch (CosXmlClientException clientException) {clientException.printStackTrace();} catch (CosXmlServiceException serviceException) {serviceException.printStackTrace();}
// Bucket name in the format of BucketName-APPID (APPID is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucketString bucket = "examplebucket-1250000000";String cosPath = "exampleobject"; // Location identifier of the object in the bucket, i.e., the object keyDeleteObjectTaggingRequest deleteObjectTaggingRequest = new DeleteObjectTaggingRequest(bucket, cosPath);try {DeleteObjectTaggingResult deleteObjectTaggingResult = cosXmlService.deleteObjectTagging(deleteObjectTaggingRequest);} catch (CosXmlClientException clientException) {clientException.printStackTrace();} catch (CosXmlServiceException serviceException) {serviceException.printStackTrace();}
Apakah halaman ini membantu?