Adding an Image
This section describes how to add an image using AK/SK authentication.
Replace the AK/SK in the sample code with the actual AK/SK. Use the setPath method of the AddPictureRequestReq class to specify the URL of the image you want to add. After the configuration is complete, run the code.
- The sample code for adding an image 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; /** * Add an image. */ public class RunAddPictureSolution { 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(); RunAddPictureRequest request = new RunAddPictureRequest(); request.setInstanceName("instance-name"); AddPictureRequestReq req = new AddPictureRequestReq(); //Add an image. Use setPath to configure the image URL. req.setPath("https://bucketname.obs.cn-north-4.myhuaweicloud.com/image/test1.jpg"); request.setBody(req); try { RunAddPictureResponse response = client.runAddPicture(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()); } } }
- Execute the sample code. If the image adding result is displayed on the console, the code is successfully executed. For details, see Adding an image.
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
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot