Help Center/ ImageSearch/ SDK Reference/ Using the SDK (Java)/ Modifying Image Information
Updated on 2022-02-22 GMT+08:00

Modifying Image Information

This section describes how to modify an image using AK/SK authentication.

Replace the AK/SK in the sample code with the actual AK/SK. Use the setPath or setTags methods of the RunModifyPictureReq class to change the path or tags of an image. After the configuration is complete, run the code.

  1. The sample code for modifying image information is as follows:
    import com.alibaba.fastjson.JSON;
     
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.imagesearch.v1.*;
    import com.huaweicloud.sdk.imagesearch.v1.model.*;
    import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
     
    import java.util.HashMap;
    import java.util.Map;
     
    /**
     * Modify the information about an existing image in the image library.
     */
    public class RunModifyPictureSolution {
        
        public static void main(String[] args) {
            //Set the obtained AK and SK.
            String ak = "<YOUR AK>";
            String sk = "<YOUR SK>";
            
            ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
            
            ImageSearchClient client = ImageSearchClient.newBuilder()
                .withCredential(auth)
                //Set the region. In this example, CN North-Beijing4 is used.
                .withRegion(ImageSearchRegion.valueOf("cn-north-4"))
                .build();
            RunModifyPictureRequest request = new RunModifyPictureRequest();
            //Set the instance name.
            request.setInstanceName("instance-name");
            RunModifyPictureReq body = new RunModifyPictureReq();
            //Image URL, which is used as the ID of an image in the image library.
            body.setPath("https://bucketname.obs.cn-north-4.myhuaweicloud.com/search-test.jpg");
            //Customize tags in the form of a key-value pair.
            Map<String, String> tags = new HashMap<>();
            tags.put("animal", "dog");
            body.setTags(tags);
            request.setBody(body);
    
            try {
                RunModifyPictureResponse response = client.runModifyPicture(request);
                System.out.println(JSON.toJSONString(response));
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
  2. Execute the sample code. If the image information modification result is displayed on the console, the code is successfully executed. For details, see Modifying Image Information.
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    {"httpStatusCode":200,"result":"Success."}
     
    Process finished with exit code 0