Updated on 2022-02-22 GMT+08:00

Creating an Instance

This section describes how to create an instance using AK/SK authentication.

Replace the AK/SK in the sample code with the actual AK/SK. Use the setName and setModel methods of the CreateInstanceReq class to configure the instance name and model. After the configuration is complete, run the code.

  1. The sample code for creating an instance 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.ImageSearchClient;
    import com.huaweicloud.sdk.imagesearch.v1.model.RunCreateInstanceRequest;
    import com.huaweicloud.sdk.imagesearch.v1.model.CreateInstanceReq;
    import com.huaweicloud.sdk.imagesearch.v1.model.RunCreateInstanceResponse;
    import com.huaweicloud.sdk.imagesearch.v1.region.ImageSearchRegion;
    import java.util.Arrays;
    import java.util.List;
     
    /**
     * Create an instance. An index library is created in the instance to store image features.
     */
    public class RunCreateInstanceSolution {
        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();
            RunCreateInstanceRequest request = new RunCreateInstanceRequest();
            CreateInstanceReq body = new CreateInstanceReq();
            //Set the instance name.
            body.setName("instance-name");
            //Set the model.
            body.setModel("common-search");
            List<String> tags = Arrays.asList("animal", "plant");
            body.setTags(tags);
            request.setBody(body);
            try {
                RunCreateInstanceResponse response = client.runCreateInstance(request);
                System.out.println(JSON.toJSONString(response));
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
  2. Execute the sample code. If 200 is displayed on the console, the code is successfully executed. For details, see Creating an Instance.
    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.
    {"desc":"","domain":"general image search","expiredDate":-1,"httpStatusCode":200,"instanceName":"instance-name","level":30000000,"registerDate":1636700045229,"status":{"value":"NORMAL"}},"tags":["animal","plant"]}
     
    Process finished with exit code 0