├── .gitignore ├── README.md ├── aip ├── censor │ └── ContentCensorClient.go └── imagesearch │ └── ImageSearchClient.go ├── baseClient └── BaseClient.go ├── go.mod ├── test ├── imagesearch │ └── ImageSearchClientTest.go └── resources │ └── image │ └── baidu_image.png └── util └── FileUtil.go /.gitignore: -------------------------------------------------------------------------------- 1 | .svn 2 | .tmp 3 | .download 4 | output 5 | .*.swp 6 | .*.swo 7 | .idea/ 8 | .DS_Store 9 | 10 | main.go 11 | test/resources/AkSkConst.go -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # golang-sdk 2 | 3 | ## 目录结构 4 | ``` 5 | ├── README.md 6 | ├── aip // 应用目录 7 | │ ├── censor // 内容审核 8 | │ └── imagesearch // 图像搜索 9 | ├── baseClient // 基础类 10 | ├── test // 单元测试 11 | │ ├── imagesearch // 图像搜索单元测试 12 | │ └── resources // 资源文件 13 | └── util // 工具类 14 | ``` 15 | **建议1.1以上版本** 16 | 17 | # 使用文档 18 | 19 | 参考[官方网站](http://ai.baidu.com/docs#/Begin/top) 20 | -------------------------------------------------------------------------------- /aip/censor/ContentCensorClient.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 baidu 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package censor 18 | 19 | import ( 20 | "strconv" 21 | 22 | "github.com/Baidu-AIP/golang-sdk/baseClient" 23 | ) 24 | 25 | const __imageCensorUserDefinedUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined" 26 | 27 | const __textCensorUserDefinedUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined" 28 | 29 | const __voiceCensorUserDefinedUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/voice_censor/v3/user_defined" 30 | 31 | const __videoCensorUserDefinedUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/video_censor/v2/user_defined" 32 | 33 | type ContentCensorClient struct { 34 | auth baseClient.Auth 35 | } 36 | 37 | func NewClient(ak string, sk string) *ContentCensorClient { 38 | var client ContentCensorClient 39 | client.auth = baseClient.Auth{} 40 | client.auth.InitAuth(ak, sk) 41 | return &client 42 | } 43 | 44 | func NewCloudClient(ak string, sk string) *ContentCensorClient { 45 | var client ContentCensorClient 46 | client.auth = baseClient.Auth{} 47 | client.auth.InitCloudAuth(ak, sk) 48 | return &client 49 | } 50 | 51 | func (client *ContentCensorClient) TextCensor(text string) (result string) { 52 | data := make(map[string]string) 53 | data["text"] = text 54 | return baseClient.PostUrlForm(__textCensorUserDefinedUrl, data, &client.auth) 55 | } 56 | 57 | func (client *ContentCensorClient) ImgCensor(image string, options map[string]interface{}) (result string) { 58 | data := make(map[string]string) 59 | data["image"] = image 60 | for key, val := range options { 61 | switch val := val.(type) { 62 | case int: 63 | data[key] = strconv.Itoa(val) 64 | } 65 | } 66 | return baseClient.PostUrlForm(__imageCensorUserDefinedUrl, data, &client.auth) 67 | } 68 | 69 | func (client *ContentCensorClient) ImgCensorUrl(imgUrl string, options map[string]interface{}) (result string) { 70 | data := make(map[string]string) 71 | data["imgUrl"] = imgUrl 72 | for key, val := range options { 73 | switch val := val.(type) { 74 | case int: 75 | data[key] = strconv.Itoa(val) 76 | } 77 | } 78 | return baseClient.PostUrlForm(__imageCensorUserDefinedUrl, data, &client.auth) 79 | } 80 | 81 | func (client *ContentCensorClient) VoiceCensorUrl(url string, rate int, fmt string, options map[string]interface{}) (result string) { 82 | data := make(map[string]string) 83 | data["url"] = url 84 | data["fmt"] = fmt 85 | data["rate"] = strconv.FormatInt(int64(rate), 10) 86 | for key, val := range options { 87 | switch val := val.(type) { 88 | case bool: 89 | data[key] = strconv.FormatBool(val) 90 | } 91 | } 92 | return baseClient.PostUrlForm(__voiceCensorUserDefinedUrl, data, &client.auth) 93 | } 94 | 95 | func (client *ContentCensorClient) VoiceCensor(base64 string, rate int, fmt string, options map[string]interface{}) (result string) { 96 | data := make(map[string]string) 97 | data["base64"] = base64 98 | data["fmt"] = fmt 99 | data["rate"] = strconv.FormatInt(int64(rate), 10) 100 | for key, val := range options { 101 | switch val := val.(type) { 102 | case bool: 103 | data[key] = strconv.FormatBool(val) 104 | } 105 | } 106 | return baseClient.PostUrlForm(__voiceCensorUserDefinedUrl, data, &client.auth) 107 | } 108 | 109 | func (client *ContentCensorClient) VideoCensor(name string, videoUrl string, extId string, options map[string]interface{}) (result string) { 110 | data := make(map[string]string) 111 | data["name"] = name 112 | data["videoUrl"] = videoUrl 113 | data["extId"] = extId 114 | for key, val := range options { 115 | switch val := val.(type) { 116 | case string: 117 | data[key] = val 118 | } 119 | } 120 | return baseClient.PostUrlForm(__videoCensorUserDefinedUrl, data, &client.auth) 121 | } 122 | -------------------------------------------------------------------------------- /aip/imagesearch/ImageSearchClient.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 baidu 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package imagesearch 18 | 19 | import ( 20 | 21 | "strconv" 22 | 23 | "github.com/Baidu-AIP/golang-sdk/baseClient" 24 | ) 25 | 26 | const __imageSearchSameHqAdd = "https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/add" 27 | 28 | const __imageSearchSameHqSearch = "https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/search" 29 | 30 | const __imageSearchSameHqUpdate = "https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/update" 31 | 32 | const __imageSearchSameHqDelete = "https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/delete" 33 | 34 | const __imageSearchSimilarAdd = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add" 35 | 36 | const __imageSearchSimilarSearch = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/search" 37 | 38 | const __imageSearchSimilarUpdate = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/update" 39 | 40 | const __imageSearchSimilarDelete = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/delete" 41 | 42 | const __imageSearchProductAdd = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/product/add" 43 | 44 | const __imageSearchProductSearch = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/product/search" 45 | 46 | const __imageSearchProductUpdate = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/product/update" 47 | 48 | const __imageSearchProductDelete = "https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/product/delete" 49 | 50 | const __imageSearchPictureBookAdd = "https://aip.baidubce.com/rest/2.0/imagesearch/v1/realtime_search/picturebook/add" 51 | 52 | const __imageSearchPictureBookSearch = "https://aip.baidubce.com/rest/2.0/imagesearch/v1/realtime_search/picturebook/search" 53 | 54 | const __imageSearchPictureBookDelete = "https://aip.baidubce.com/rest/2.0/imagesearch/v1/realtime_search/picturebook/delete" 55 | 56 | const __imageSearchPictureBookUpdate = "https://aip.baidubce.com/rest/2.0/imagesearch/v1/realtime_search/picturebook/update" 57 | 58 | 59 | type ImageSearchClient struct { 60 | auth baseClient.Auth 61 | } 62 | 63 | func NewClient(ak string, sk string) *ImageSearchClient { 64 | var client ImageSearchClient 65 | client.auth = baseClient.Auth{} 66 | client.auth.InitAuth(ak, sk) 67 | return &client 68 | } 69 | 70 | func NewCloudClient(ak string, sk string) *ImageSearchClient { 71 | var client ImageSearchClient 72 | client.auth = baseClient.Auth{} 73 | client.auth.InitCloudAuth(ak, sk) 74 | return &client 75 | } 76 | 77 | // SimilarAdd 相似图片搜索 - 入库 78 | func (client *ImageSearchClient) SimilarAdd(image string, brief string, 79 | options map[string]interface{}) (result string) { 80 | data := make(map[string]string) 81 | 82 | data["image"] = image 83 | data["brief"] = brief 84 | 85 | for key, val := range options { 86 | switch val := val.(type) { 87 | case int: 88 | data[key] = strconv.Itoa(val) 89 | } 90 | } 91 | 92 | return baseClient.PostUrlForm(__imageSearchSimilarAdd, data, &client.auth) 93 | } 94 | 95 | // SimilarAddUrl 相似图片搜索 - 入库 96 | func (client *ImageSearchClient) SimilarAddUrl(url string, brief string, 97 | options map[string]interface{}) (result string) { 98 | data := make(map[string]string) 99 | 100 | data["url"] = url 101 | data["brief"] = brief 102 | 103 | for key, val := range options { 104 | switch val := val.(type) { 105 | case int: 106 | data[key] = strconv.Itoa(val) 107 | } 108 | } 109 | 110 | return baseClient.PostUrlForm(__imageSearchSimilarAdd, data, &client.auth) 111 | } 112 | 113 | // SimilarSearch 相似图片搜索 - 检索 114 | func (client *ImageSearchClient) SimilarSearch(image string, options map[string]interface{}) (result string) { 115 | data := make(map[string]string) 116 | 117 | data["image"] = image 118 | 119 | for key, val := range options { 120 | switch val := val.(type) { 121 | case int: 122 | data[key] = strconv.Itoa(val) 123 | } 124 | } 125 | 126 | return baseClient.PostUrlForm(__imageSearchSimilarSearch, data, &client.auth) 127 | } 128 | 129 | // SimilarSearchUrl 相似图片搜索 - 检索 130 | func (client *ImageSearchClient) SimilarSearchUrl(url string, options map[string]interface{}) (result string) { 131 | data := make(map[string]string) 132 | 133 | data["url"] = url 134 | 135 | for key, val := range options { 136 | switch val := val.(type) { 137 | case int: 138 | data[key] = strconv.Itoa(val) 139 | } 140 | } 141 | 142 | return baseClient.PostUrlForm(__imageSearchSimilarSearch, data, &client.auth) 143 | } 144 | 145 | // SimilarDelete 相似图片搜索 - 删除 146 | func (client *ImageSearchClient) SimilarDelete(image string, options map[string]interface{}) (result string) { 147 | data := make(map[string]string) 148 | 149 | data["image"] = image 150 | 151 | for key, val := range options { 152 | switch val := val.(type) { 153 | case int: 154 | data[key] = strconv.Itoa(val) 155 | } 156 | } 157 | 158 | return baseClient.PostUrlForm(__imageSearchSimilarDelete, data, &client.auth) 159 | } 160 | 161 | // SimilarDeleteUrl 相似图片搜索 - 删除 162 | func (client *ImageSearchClient) SimilarDeleteUrl(url string, options map[string]interface{}) (result string) { 163 | data := make(map[string]string) 164 | 165 | data["url"] = url 166 | 167 | for key, val := range options { 168 | switch val := val.(type) { 169 | case int: 170 | data[key] = strconv.Itoa(val) 171 | } 172 | } 173 | 174 | return baseClient.PostUrlForm(__imageSearchSimilarDelete, data, &client.auth) 175 | } 176 | 177 | // SimilarDeleteSign 相似图片搜索 - 删除 178 | func (client *ImageSearchClient) SimilarDeleteSign(contSign string, options map[string]interface{}) (result string) { 179 | data := make(map[string]string) 180 | 181 | data["cont_sign"] = contSign 182 | 183 | for key, val := range options { 184 | switch val := val.(type) { 185 | case int: 186 | data[key] = strconv.Itoa(val) 187 | } 188 | } 189 | 190 | return baseClient.PostUrlForm(__imageSearchSimilarDelete, data, &client.auth) 191 | } 192 | 193 | // SimilarUpdate 相似图片搜索 - 更新 194 | func (client *ImageSearchClient) SimilarUpdate(image string, options map[string]interface{}) (result string) { 195 | data := make(map[string]string) 196 | 197 | data["image"] = image 198 | 199 | for key, val := range options { 200 | switch val := val.(type) { 201 | case int: 202 | data[key] = strconv.Itoa(val) 203 | } 204 | } 205 | 206 | return baseClient.PostUrlForm(__imageSearchSimilarUpdate, data, &client.auth) 207 | } 208 | 209 | // SimilarUpdateUrl 相似图片搜索 - 更新 210 | func (client *ImageSearchClient) SimilarUpdateUrl(url string, options map[string]interface{}) (result string) { 211 | data := make(map[string]string) 212 | 213 | data["url"] = url 214 | 215 | for key, val := range options { 216 | switch val := val.(type) { 217 | case int: 218 | data[key] = strconv.Itoa(val) 219 | } 220 | } 221 | 222 | return baseClient.PostUrlForm(__imageSearchSimilarUpdate, data, &client.auth) 223 | } 224 | 225 | // SimilarUpdateSign 相似图片搜索 - 更新 226 | func (client *ImageSearchClient) SimilarUpdateSign(contSign string, options map[string]interface{}) (result string) { 227 | data := make(map[string]string) 228 | 229 | data["cont_sign"] = contSign 230 | 231 | for key, val := range options { 232 | switch val := val.(type) { 233 | case int: 234 | data[key] = strconv.Itoa(val) 235 | } 236 | } 237 | 238 | return baseClient.PostUrlForm(__imageSearchSimilarUpdate, data, &client.auth) 239 | } 240 | 241 | // SameHqAdd 相同图片搜索 - 入库 242 | func (client *ImageSearchClient) SameHqAdd(image string, brief string, 243 | options map[string]interface{}) (result string) { 244 | data := make(map[string]string) 245 | 246 | data["image"] = image 247 | data["brief"] = brief 248 | 249 | for key, val := range options { 250 | switch val := val.(type) { 251 | case int: 252 | data[key] = strconv.Itoa(val) 253 | } 254 | } 255 | 256 | return baseClient.PostUrlForm(__imageSearchSameHqAdd, data, &client.auth) 257 | } 258 | 259 | // SameHqAddUrl 相同图片搜索 - 入库 260 | func (client *ImageSearchClient) SameHqAddUrl(image string, brief string, 261 | options map[string]interface{}) (result string) { 262 | data := make(map[string]string) 263 | 264 | data["image"] = image 265 | data["brief"] = brief 266 | 267 | for key, val := range options { 268 | switch val := val.(type) { 269 | case int: 270 | data[key] = strconv.Itoa(val) 271 | } 272 | } 273 | 274 | return baseClient.PostUrlForm(__imageSearchSameHqAdd, data, &client.auth) 275 | } 276 | 277 | // SameHqSearch 相同图片搜索 - 检索 278 | func (client *ImageSearchClient) SameHqSearch(image string, options map[string]interface{}) (result string) { 279 | data := make(map[string]string) 280 | 281 | data["image"] = image 282 | 283 | for key, val := range options { 284 | switch val := val.(type) { 285 | case int: 286 | data[key] = strconv.Itoa(val) 287 | } 288 | } 289 | 290 | return baseClient.PostUrlForm(__imageSearchSameHqSearch, data, &client.auth) 291 | } 292 | 293 | // SameHqSearchUrl 相同图片搜索 - 检索 294 | func (client *ImageSearchClient) SameHqSearchUrl(url string, options map[string]interface{}) (result string) { 295 | data := make(map[string]string) 296 | 297 | data["url"] = url 298 | 299 | for key, val := range options { 300 | switch val := val.(type) { 301 | case int: 302 | data[key] = strconv.Itoa(val) 303 | } 304 | } 305 | 306 | return baseClient.PostUrlForm(__imageSearchSameHqSearch, data, &client.auth) 307 | } 308 | 309 | // SameHqDelete 相同图片搜索 - 删除 310 | func (client *ImageSearchClient) SameHqDelete(image string, options map[string]interface{}) (result string) { 311 | data := make(map[string]string) 312 | 313 | data["image"] = image 314 | 315 | for key, val := range options { 316 | switch val := val.(type) { 317 | case int: 318 | data[key] = strconv.Itoa(val) 319 | } 320 | } 321 | 322 | return baseClient.PostUrlForm(__imageSearchSameHqDelete, data, &client.auth) 323 | } 324 | 325 | // SameHqDeleteUrl 相同图片搜索 - 删除 326 | func (client *ImageSearchClient) SameHqDeleteUrl(url string, options map[string]interface{}) (result string) { 327 | data := make(map[string]string) 328 | 329 | data["url"] = url 330 | 331 | for key, val := range options { 332 | switch val := val.(type) { 333 | case int: 334 | data[key] = strconv.Itoa(val) 335 | } 336 | } 337 | 338 | return baseClient.PostUrlForm(__imageSearchSameHqDelete, data, &client.auth) 339 | } 340 | 341 | // SameHqDeleteSign 相同图片搜索 - 删除 342 | func (client *ImageSearchClient) SameHqDeleteSign(contSign string, options map[string]interface{}) (result string) { 343 | data := make(map[string]string) 344 | 345 | data["cont_sign"] = contSign 346 | 347 | for key, val := range options { 348 | switch val := val.(type) { 349 | case int: 350 | data[key] = strconv.Itoa(val) 351 | } 352 | } 353 | 354 | return baseClient.PostUrlForm(__imageSearchSameHqDelete, data, &client.auth) 355 | } 356 | 357 | // SameHqUpdate 相同图片搜索 - 更新 358 | func (client *ImageSearchClient) SameHqUpdate(image string, options map[string]interface{}) (result string) { 359 | data := make(map[string]string) 360 | 361 | data["image"] = image 362 | 363 | for key, val := range options { 364 | switch val := val.(type) { 365 | case int: 366 | data[key] = strconv.Itoa(val) 367 | } 368 | } 369 | 370 | return baseClient.PostUrlForm(__imageSearchSameHqUpdate, data, &client.auth) 371 | } 372 | 373 | // SameHqUpdateUrl 相同图片搜索 - 更新 374 | func (client *ImageSearchClient) SameHqUpdateUrl(url string, options map[string]interface{}) (result string) { 375 | data := make(map[string]string) 376 | 377 | data["url"] = url 378 | 379 | for key, val := range options { 380 | switch val := val.(type) { 381 | case int: 382 | data[key] = strconv.Itoa(val) 383 | } 384 | } 385 | 386 | return baseClient.PostUrlForm(__imageSearchSameHqUpdate, data, &client.auth) 387 | } 388 | 389 | // SameHqUpdateSign 相同图片搜索 - 更新 390 | func (client *ImageSearchClient) SameHqUpdateSign(contSign string, options map[string]interface{}) (result string) { 391 | data := make(map[string]string) 392 | 393 | data["cont_sign"] = contSign 394 | 395 | for key, val := range options { 396 | switch val := val.(type) { 397 | case int: 398 | data[key] = strconv.Itoa(val) 399 | } 400 | } 401 | 402 | return baseClient.PostUrlForm(__imageSearchSameHqUpdate, data, &client.auth) 403 | } 404 | 405 | // ProductAdd 商品图片搜索 - 入库 406 | func (client *ImageSearchClient) ProductAdd(image string, brief string, 407 | options map[string]interface{}) (result string) { 408 | data := make(map[string]string) 409 | 410 | data["image"] = image 411 | data["brief"] = brief 412 | 413 | for key, val := range options { 414 | switch val := val.(type) { 415 | case int: 416 | data[key] = strconv.Itoa(val) 417 | } 418 | } 419 | 420 | return baseClient.PostUrlForm(__imageSearchProductAdd, data, &client.auth) 421 | } 422 | 423 | // ProductAddUrl 商品图片搜索 - 入库 424 | func (client *ImageSearchClient) ProductAddUrl(url string, brief string, 425 | options map[string]interface{}) (result string) { 426 | data := make(map[string]string) 427 | 428 | data["url"] = url 429 | data["brief"] = brief 430 | 431 | for key, val := range options { 432 | switch val := val.(type) { 433 | case int: 434 | data[key] = strconv.Itoa(val) 435 | } 436 | } 437 | 438 | return baseClient.PostUrlForm(__imageSearchProductAdd, data, &client.auth) 439 | } 440 | 441 | // ProductSearch 商品图片搜索 - 检索 442 | func (client *ImageSearchClient) ProductSearch(image string, options map[string]interface{}) (result string) { 443 | data := make(map[string]string) 444 | 445 | data["image"] = image 446 | 447 | for key, val := range options { 448 | switch val := val.(type) { 449 | case int: 450 | data[key] = strconv.Itoa(val) 451 | } 452 | } 453 | 454 | return baseClient.PostUrlForm(__imageSearchProductSearch, data, &client.auth) 455 | } 456 | 457 | // ProductSearchUrl 商品图片搜索 - 检索 458 | func (client *ImageSearchClient) ProductSearchUrl(url string, options map[string]interface{}) (result string) { 459 | data := make(map[string]string) 460 | 461 | data["url"] = url 462 | 463 | for key, val := range options { 464 | switch val := val.(type) { 465 | case int: 466 | data[key] = strconv.Itoa(val) 467 | } 468 | } 469 | 470 | return baseClient.PostUrlForm(__imageSearchProductSearch, data, &client.auth) 471 | } 472 | 473 | // ProductUpdate 商品图片搜索 - 更新 474 | func (client *ImageSearchClient) ProductUpdate(image string, options map[string]interface{}) (result string) { 475 | data := make(map[string]string) 476 | 477 | data["image"] = image 478 | 479 | for key, val := range options { 480 | switch val := val.(type) { 481 | case int: 482 | data[key] = strconv.Itoa(val) 483 | } 484 | } 485 | 486 | return baseClient.PostUrlForm(__imageSearchProductUpdate, data, &client.auth) 487 | } 488 | 489 | // ProductUpdateUrl 商品图片搜索 - 更新 490 | func (client *ImageSearchClient) ProductUpdateUrl(url string, options map[string]interface{}) (result string) { 491 | data := make(map[string]string) 492 | 493 | data["url"] = url 494 | 495 | for key, val := range options { 496 | switch val := val.(type) { 497 | case int: 498 | data[key] = strconv.Itoa(val) 499 | } 500 | } 501 | 502 | return baseClient.PostUrlForm(__imageSearchProductUpdate, data, &client.auth) 503 | } 504 | 505 | // ProductUpdateSign 商品图片搜索 - 更新 506 | func (client *ImageSearchClient) ProductUpdateSign(contSign string, options map[string]interface{}) (result string) { 507 | data := make(map[string]string) 508 | 509 | data["cont_sign"] = contSign 510 | 511 | for key, val := range options { 512 | switch val := val.(type) { 513 | case int: 514 | data[key] = strconv.Itoa(val) 515 | } 516 | } 517 | 518 | return baseClient.PostUrlForm(__imageSearchProductUpdate, data, &client.auth) 519 | } 520 | 521 | // ProductDelete 商品图片搜索 - 删除 522 | func (client *ImageSearchClient) ProductDelete(image string, options map[string]interface{}) (result string) { 523 | data := make(map[string]string) 524 | 525 | data["image"] = image 526 | 527 | for key, val := range options { 528 | switch val := val.(type) { 529 | case int: 530 | data[key] = strconv.Itoa(val) 531 | } 532 | } 533 | 534 | return baseClient.PostUrlForm(__imageSearchProductDelete, data, &client.auth) 535 | } 536 | 537 | // ProductDeleteUrl 商品图片搜索 - 删除 538 | func (client *ImageSearchClient) ProductDeleteUrl(url string, options map[string]interface{}) (result string) { 539 | data := make(map[string]string) 540 | 541 | data["url"] = url 542 | 543 | for key, val := range options { 544 | switch val := val.(type) { 545 | case int: 546 | data[key] = strconv.Itoa(val) 547 | } 548 | } 549 | 550 | return baseClient.PostUrlForm(__imageSearchProductDelete, data, &client.auth) 551 | } 552 | 553 | // ProductDeleteSign 商品图片搜索 - 删除 554 | func (client *ImageSearchClient) ProductDeleteSign(contSign string, options map[string]interface{}) (result string) { 555 | data := make(map[string]string) 556 | 557 | data["cont_sign"] = contSign 558 | 559 | for key, val := range options { 560 | switch val := val.(type) { 561 | case int: 562 | data[key] = strconv.Itoa(val) 563 | } 564 | } 565 | 566 | return baseClient.PostUrlForm(__imageSearchProductDelete, data, &client.auth) 567 | } 568 | 569 | // PictureBookAdd 绘本图片搜索 - 入库 570 | func (client *ImageSearchClient) PictureBookAdd(image string, brief string, 571 | options map[string]interface{}) (result string) { 572 | data := make(map[string]string) 573 | 574 | data["image"] = image 575 | data["brief"] = brief 576 | 577 | for key, val := range options { 578 | switch val := val.(type) { 579 | case int: 580 | data[key] = strconv.Itoa(val) 581 | } 582 | } 583 | 584 | return baseClient.PostUrlForm(__imageSearchPictureBookAdd, data, &client.auth) 585 | } 586 | 587 | // PictureBookAddUrl 绘本图片搜索 - 入库 588 | func (client *ImageSearchClient) PictureBookAddUrl(url string, brief string, 589 | options map[string]interface{}) (result string) { 590 | data := make(map[string]string) 591 | 592 | data["url"] = url 593 | data["brief"] = brief 594 | 595 | for key, val := range options { 596 | switch val := val.(type) { 597 | case int: 598 | data[key] = strconv.Itoa(val) 599 | } 600 | } 601 | 602 | return baseClient.PostUrlForm(__imageSearchPictureBookAdd, data, &client.auth) 603 | } 604 | 605 | // PictureBookSearch 绘本图片搜索 - 检索 606 | func (client *ImageSearchClient) PictureBookSearch(image string, options map[string]interface{}) (result string) { 607 | data := make(map[string]string) 608 | 609 | data["image"] = image 610 | 611 | for key, val := range options { 612 | switch val := val.(type) { 613 | case int: 614 | data[key] = strconv.Itoa(val) 615 | } 616 | } 617 | 618 | return baseClient.PostUrlForm(__imageSearchPictureBookSearch, data, &client.auth) 619 | } 620 | 621 | // PictureBookSearchUrl 绘本图片搜索 - 检索 622 | func (client *ImageSearchClient) PictureBookSearchUrl(url string, options map[string]interface{}) (result string) { 623 | data := make(map[string]string) 624 | 625 | data["url"] = url 626 | 627 | for key, val := range options { 628 | switch val := val.(type) { 629 | case int: 630 | data[key] = strconv.Itoa(val) 631 | } 632 | } 633 | 634 | return baseClient.PostUrlForm(__imageSearchPictureBookSearch, data, &client.auth) 635 | } 636 | 637 | // PictureBookDelete 绘本图片搜索 - 删除 638 | func (client *ImageSearchClient) PictureBookDelete(image string, options map[string]interface{}) (result string) { 639 | data := make(map[string]string) 640 | 641 | data["image"] = image 642 | 643 | for key, val := range options { 644 | switch val := val.(type) { 645 | case int: 646 | data[key] = strconv.Itoa(val) 647 | } 648 | } 649 | 650 | return baseClient.PostUrlForm(__imageSearchPictureBookDelete, data, &client.auth) 651 | } 652 | 653 | // PictureBookDeleteUrl 绘本图片搜索 - 删除 654 | func (client *ImageSearchClient) PictureBookDeleteUrl(url string, options map[string]interface{}) (result string) { 655 | data := make(map[string]string) 656 | 657 | data["url"] = url 658 | 659 | for key, val := range options { 660 | switch val := val.(type) { 661 | case int: 662 | data[key] = strconv.Itoa(val) 663 | } 664 | } 665 | 666 | return baseClient.PostUrlForm(__imageSearchPictureBookDelete, data, &client.auth) 667 | } 668 | 669 | // PictureBookDeleteSign 绘本图片搜索 - 删除 670 | func (client *ImageSearchClient) PictureBookDeleteSign(contSign string, options map[string]interface{}) (result string) { 671 | data := make(map[string]string) 672 | 673 | data["cont_sign"] = contSign 674 | 675 | for key, val := range options { 676 | switch val := val.(type) { 677 | case int: 678 | data[key] = strconv.Itoa(val) 679 | } 680 | } 681 | 682 | return baseClient.PostUrlForm(__imageSearchPictureBookDelete, data, &client.auth) 683 | } 684 | 685 | // PictureBookUpdate 绘本图片搜索 - 更新 686 | func (client *ImageSearchClient) PictureBookUpdate(image string, options map[string]interface{}) (result string) { 687 | data := make(map[string]string) 688 | 689 | data["image"] = image 690 | 691 | for key, val := range options { 692 | switch val := val.(type) { 693 | case int: 694 | data[key] = strconv.Itoa(val) 695 | } 696 | } 697 | 698 | return baseClient.PostUrlForm(__imageSearchPictureBookUpdate, data, &client.auth) 699 | } 700 | 701 | // PictureBookUpdateUrl 绘本图片搜索 - 更新 702 | func (client *ImageSearchClient) PictureBookUpdateUrl(url string, options map[string]interface{}) (result string) { 703 | data := make(map[string]string) 704 | 705 | data["url"] = url 706 | 707 | for key, val := range options { 708 | switch val := val.(type) { 709 | case int: 710 | data[key] = strconv.Itoa(val) 711 | } 712 | } 713 | 714 | return baseClient.PostUrlForm(__imageSearchPictureBookUpdate, data, &client.auth) 715 | } 716 | 717 | // PictureBookUpdateSign 绘本图片搜索 - 更新 718 | func (client *ImageSearchClient) PictureBookUpdateSign(contSign string, options map[string]interface{}) (result string) { 719 | data := make(map[string]string) 720 | 721 | data["cont_sign"] = contSign 722 | 723 | for key, val := range options { 724 | switch val := val.(type) { 725 | case int: 726 | data[key] = strconv.Itoa(val) 727 | } 728 | } 729 | 730 | return baseClient.PostUrlForm(__imageSearchPictureBookUpdate, data, &client.auth) 731 | } 732 | 733 | 734 | 735 | -------------------------------------------------------------------------------- /baseClient/BaseClient.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 baidu 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package baseClient 18 | 19 | import ( 20 | "bytes" 21 | "crypto/hmac" 22 | "crypto/sha256" 23 | "encoding/hex" 24 | "encoding/json" 25 | "fmt" 26 | "io" 27 | "io/ioutil" 28 | "log" 29 | "net/http" 30 | "net/url" 31 | "sort" 32 | "strings" 33 | "time" 34 | ) 35 | 36 | const authUrl = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s" 37 | 38 | type Auth struct { 39 | accessToken string 40 | times int64 41 | hasAuth bool 42 | client http.Client 43 | error string 44 | isCloudUser bool 45 | ak string 46 | sk string 47 | i int8 48 | } 49 | 50 | func (auth *Auth) InitAuth(ak string, sk string) { 51 | auth.ak = ak 52 | auth.sk = sk 53 | auth.client = http.Client{ 54 | Timeout: time.Second * 20, 55 | } 56 | auth.refresh() 57 | } 58 | 59 | func (auth *Auth) InitCloudAuth(ak string, sk string) { 60 | auth.ak = ak 61 | auth.sk = sk 62 | auth.client = http.Client{ 63 | Timeout: time.Second * 20, 64 | } 65 | auth.isCloudUser = true 66 | } 67 | func (auth *Auth) refresh() { 68 | log.Println("get access_token") 69 | now := time.Now().Unix() 70 | url := fmt.Sprintf(authUrl, auth.ak, auth.sk) 71 | resp, err := auth.client.Get(url) 72 | if err != nil || resp == nil { 73 | auth.hasAuth = false 74 | log.Println(err) 75 | return 76 | } 77 | defer func(Body io.ReadCloser) { 78 | _ = Body.Close() 79 | }(resp.Body) 80 | body, _ := ioutil.ReadAll(resp.Body) 81 | var jsonMap map[string]interface{} 82 | err = json.Unmarshal(body, &jsonMap) 83 | if err != nil { 84 | log.Println("%s is not json", string(body)) 85 | return 86 | } 87 | accessToken, ok := jsonMap["access_token"] 88 | if !ok { 89 | auth.hasAuth = false 90 | log.Println(string(body)) 91 | return 92 | } 93 | auth.accessToken = accessToken.(string) 94 | expires := int64(jsonMap["expires_in"].(float64)) 95 | auth.times = expires + now - 3600 96 | auth.hasAuth = true 97 | auth.error = "" 98 | } 99 | 100 | func hmacSha256(data string, secret string) string { 101 | h := hmac.New(sha256.New, []byte(secret)) 102 | h.Write([]byte(data)) 103 | return hex.EncodeToString(h.Sum(nil)) 104 | } 105 | 106 | func (auth *Auth) setHeader(req *http.Request) { 107 | now := time.Now().UTC().Format("2006-01-02T15:04:05Z") 108 | req.Header.Set("Host", req.Host) 109 | req.Header.Set("x-bce-date", now) 110 | signedHeaders := "host;x-bce-date" 111 | heades := []string{} 112 | heades = append(heades, "host:"+url.QueryEscape(req.Host)) 113 | heades = append(heades, "x-bce-date:"+url.QueryEscape(now)) 114 | sort.Strings(heades) 115 | canonicalHeaders := strings.Join(heades, "\n") 116 | expire := "1800" 117 | val := fmt.Sprintf("bce-auth-v1/%s/%s/%s", auth.ak, now, expire) 118 | signingKey := hmacSha256(val, auth.sk) 119 | 120 | canonicalURI := req.URL.Path 121 | if canonicalURI == "" { 122 | canonicalURI = "/" 123 | } 124 | canonicalURI = url.QueryEscape(canonicalURI) 125 | canonicalURI = strings.ReplaceAll(canonicalURI, "%2F", "/") 126 | canonicalQueryString := req.URL.RawQuery 127 | if canonicalQueryString != "" { 128 | queryArray := strings.Split(canonicalQueryString, "&") 129 | stringList := []string{} 130 | for _, val := range queryArray { 131 | kvArray := strings.Split(val, "=") 132 | kv := "" 133 | key := kvArray[0] 134 | kv += url.QueryEscape(key) + "=" 135 | if len(kvArray) >= 2 { 136 | value := kvArray[1] 137 | kv += url.QueryEscape(value) 138 | } 139 | stringList = append(stringList, kv) 140 | } 141 | sort.Strings(stringList) 142 | canonicalQueryString = strings.Join(stringList, "&") 143 | } 144 | canonicalRequest := req.Method + "\n" + canonicalURI + "\n" + canonicalQueryString + "\n" + canonicalHeaders 145 | signature := hmacSha256(canonicalRequest, signingKey) 146 | authInfo := fmt.Sprintf("bce-auth-v1/%s/%s/%s/%s/%s", auth.ak, now, expire, signedHeaders, signature) 147 | req.Header.Set("Authorization", authInfo) 148 | } 149 | 150 | func (auth *Auth) setParam(url string) string { 151 | var buffer bytes.Buffer 152 | buffer.WriteString(url) 153 | if strings.Contains(url, "?") { 154 | buffer.WriteString("&aipSdk=golang") 155 | } else { 156 | buffer.WriteString("?aipSdk=golang") 157 | } 158 | if auth.accessToken != "" { 159 | buffer.WriteString("&access_token=") 160 | buffer.WriteString(auth.accessToken) 161 | } 162 | return buffer.String() 163 | } 164 | 165 | func PostJson(urlString string, data string, auth *Auth) string { 166 | now := time.Now().Unix() 167 | if auth.hasAuth && auth.times < now && !auth.isCloudUser { 168 | auth.refresh() 169 | } 170 | urlString = auth.setParam(urlString) 171 | req, err := http.NewRequest("POST", urlString, bytes.NewBuffer([]byte(data))) 172 | req.Header.Set("Content-Type", "application/json") 173 | auth.setHeader(req) 174 | client := auth.client 175 | resp, err := client.Do(req) 176 | if err != nil { 177 | return err.Error() 178 | } 179 | defer func(Body io.ReadCloser) { 180 | _ = Body.Close() 181 | }(resp.Body) 182 | body, _ := ioutil.ReadAll(resp.Body) 183 | return string(body) 184 | } 185 | 186 | func PostUrlForm(urlString string, data map[string]string, auth *Auth) string { 187 | if !auth.isCloudUser { 188 | now := time.Now().Unix() 189 | if !auth.hasAuth || (auth.hasAuth && auth.times < now) { 190 | auth.refresh() 191 | } 192 | } 193 | urlString = auth.setParam(urlString) 194 | DataUrlVal := url.Values{} 195 | for key, val := range data { 196 | DataUrlVal.Add(key, val) 197 | } 198 | req, err := http.NewRequest("POST", urlString, strings.NewReader(DataUrlVal.Encode())) 199 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") 200 | if auth.isCloudUser { 201 | auth.setHeader(req) 202 | } 203 | client := auth.client 204 | resp, err := client.Do(req) 205 | if err != nil { 206 | return err.Error() 207 | } 208 | defer func(Body io.ReadCloser) { 209 | _ = Body.Close() 210 | }(resp.Body) 211 | body, _ := ioutil.ReadAll(resp.Body) 212 | var jsonMap map[string]interface{} 213 | err = json.Unmarshal(body, &jsonMap) 214 | if err != nil { 215 | log.Println("ger &s response %s is not json", urlString, string(body)) 216 | } else { 217 | errorCode, ok := jsonMap["error_code"] 218 | if ok && int(errorCode.(float64)) == 14 { 219 | auth.isCloudUser = false 220 | } 221 | } 222 | return string(body) 223 | } 224 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/Baidu-AIP/golang-sdk 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /test/imagesearch/ImageSearchClientTest.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/Baidu-AIP/golang-sdk/aip/imagesearch" 7 | 8 | "github.com/Baidu-AIP/golang-sdk/util" 9 | 10 | "github.com/Baidu-AIP/golang-sdk/test/resources" 11 | ) 12 | 13 | var image = util.ReadFileToBase64("test/resources/image/baidu_image.png") 14 | var url = "https://baidu.aip.test.com" 15 | var brief = "{\"name\":\"百度\", \"id\":\"123\"}" 16 | var options = make(map[string]interface{}) 17 | var contSign = "123" 18 | 19 | // SimilarAddTest 相似图片搜索 - 入库 20 | func SimilarAddTest(client *imagesearch.ImageSearchClient) { 21 | fmt.Println(client.SimilarAdd(image, brief, options)) 22 | } 23 | 24 | // SimilarAddUrlTest 相似图片搜索 - 入库 25 | func SimilarAddUrlTest(client *imagesearch.ImageSearchClient) { 26 | fmt.Println(client.SimilarAddUrl(url, brief, options)) 27 | } 28 | 29 | // SimilarSearchTest 相似图片搜索 - 检索 30 | func SimilarSearchTest(client *imagesearch.ImageSearchClient) { 31 | fmt.Println(client.SimilarSearch(image, options)) 32 | } 33 | 34 | // SimilarSearchUrlTest 相似图片搜索 - 检索 35 | func SimilarSearchUrlTest(client *imagesearch.ImageSearchClient) { 36 | fmt.Println(client.SimilarSearchUrl(url, options)) 37 | } 38 | 39 | // SimilarUpdateTest 相似图片搜索 - 更新 40 | func SimilarUpdateTest(client *imagesearch.ImageSearchClient) { 41 | fmt.Println(client.SimilarUpdate(image, options)) 42 | } 43 | 44 | // SimilarUpdateUrlTest 相似图片搜索 - 更新 45 | func SimilarUpdateUrlTest(client *imagesearch.ImageSearchClient) { 46 | fmt.Println(client.SimilarUpdateUrl(url, options)) 47 | } 48 | 49 | // SimilarUpdateSignTest 相似图片搜索 - 更新 50 | func SimilarUpdateSignTest(client *imagesearch.ImageSearchClient) { 51 | fmt.Println(client.SimilarUpdateSign(url, options)) 52 | } 53 | 54 | // SimilarDeleteTest 相似图片搜索 - 删除 55 | func SimilarDeleteTest(client *imagesearch.ImageSearchClient) { 56 | fmt.Println(client.SimilarDelete(image, options)) 57 | } 58 | 59 | // SimilarDeleteUrlTest 相似图片搜索 - 删除 60 | func SimilarDeleteUrlTest(client *imagesearch.ImageSearchClient) { 61 | fmt.Println(client.SimilarDeleteUrl(url, options)) 62 | } 63 | 64 | // SimilarDeleteSignTest 相似图片搜索 - 删除 65 | func SimilarDeleteSignTest(client *imagesearch.ImageSearchClient) { 66 | fmt.Println(client.SimilarDeleteSign(contSign, options)) 67 | } 68 | 69 | // ***************** 相同图片搜索 ******************** 70 | 71 | // SameHqAddTest 相同图片搜索 - 入库 72 | func SameHqAddTest(client *imagesearch.ImageSearchClient) { 73 | fmt.Println(client.SameHqAdd(image, brief, options)) 74 | } 75 | 76 | // SameHqAddUrlTest 相同图片搜索 - 入库 77 | func SameHqAddUrlTest(client *imagesearch.ImageSearchClient) { 78 | fmt.Println(client.SimilarAddUrl(url, brief, options)) 79 | } 80 | 81 | // SameHqSearchTest 相同图片搜索 - 检索 82 | func SameHqSearchTest(client *imagesearch.ImageSearchClient) { 83 | fmt.Println(client.SameHqSearch(image, options)) 84 | } 85 | 86 | // SameHqSearchUrlTest 相同图片搜索 - 检索 87 | func SameHqSearchUrlTest(client *imagesearch.ImageSearchClient) { 88 | fmt.Println(client.SameHqSearchUrl(url, options)) 89 | } 90 | 91 | // SameHqUpdateTest 相同图片搜索 - 更新 92 | func SameHqUpdateTest(client *imagesearch.ImageSearchClient) { 93 | fmt.Println(client.SameHqUpdate(image, options)) 94 | } 95 | 96 | // SameHqUpdateUrlTest 相同图片搜索 - 更新 97 | func SameHqUpdateUrlTest(client *imagesearch.ImageSearchClient) { 98 | fmt.Println(client.SameHqUpdateUrl(url, options)) 99 | } 100 | 101 | // SameHqUpdateSignTest 相同图片搜索 - 更新 102 | func SameHqUpdateSignTest(client *imagesearch.ImageSearchClient) { 103 | fmt.Println(client.SameHqUpdateSign(url, options)) 104 | } 105 | 106 | // SameHqDeleteTest 相同图片搜索 - 删除 107 | func SameHqDeleteTest(client *imagesearch.ImageSearchClient) { 108 | fmt.Println(client.SameHqDelete(image, options)) 109 | } 110 | 111 | // SameHqDeleteUrlTest 相同图片搜索 - 删除 112 | func SameHqDeleteUrlTest(client *imagesearch.ImageSearchClient) { 113 | fmt.Println(client.SimilarDeleteUrl(url, options)) 114 | } 115 | 116 | // SameHqDeleteSignTest 相同图片搜索 - 删除 117 | func SameHqDeleteSignTest(client *imagesearch.ImageSearchClient) { 118 | fmt.Println(client.SameHqDeleteSign(contSign, options)) 119 | } 120 | 121 | // ***************** 商品图片搜索 ******************** 122 | 123 | // ProductAddTest 商品图片搜索 - 入库 124 | func ProductAddTest(client *imagesearch.ImageSearchClient) { 125 | fmt.Println(client.ProductAdd(image, brief, options)) 126 | } 127 | 128 | // ProductAddUrlTest 商品图片搜索 - 入库 129 | func ProductAddUrlTest(client *imagesearch.ImageSearchClient) { 130 | fmt.Println(client.ProductAddUrl(url, brief, options)) 131 | } 132 | 133 | // ProductSearchTest 商品图片搜索 - 检索 134 | func ProductSearchTest(client *imagesearch.ImageSearchClient) { 135 | fmt.Println(client.ProductSearch(image, options)) 136 | } 137 | 138 | // ProductSearchUrlTest 商品图片搜索 - 检索 139 | func ProductSearchUrlTest(client *imagesearch.ImageSearchClient) { 140 | fmt.Println(client.ProductSearchUrl(url, options)) 141 | } 142 | 143 | // ProductUpdateTest 商品图片搜索 - 更新 144 | func ProductUpdateTest(client *imagesearch.ImageSearchClient) { 145 | fmt.Println(client.ProductUpdate(image, options)) 146 | } 147 | 148 | // ProductUpdateUrlTest 商品图片搜索 - 更新 149 | func ProductUpdateUrlTest(client *imagesearch.ImageSearchClient) { 150 | fmt.Println(client.ProductUpdateUrl(url, options)) 151 | } 152 | 153 | // ProductUpdateSignTest 商品图片搜索 - 更新 154 | func ProductUpdateSignTest(client *imagesearch.ImageSearchClient) { 155 | fmt.Println(client.ProductUpdateSign(url, options)) 156 | } 157 | 158 | // ProductDeleteTest 商品图片搜索 - 删除 159 | func ProductDeleteTest(client *imagesearch.ImageSearchClient) { 160 | fmt.Println(client.ProductDelete(image, options)) 161 | } 162 | 163 | // ProductDeleteUrlTest 商品图片搜索 - 删除 164 | func ProductDeleteUrlTest(client *imagesearch.ImageSearchClient) { 165 | fmt.Println(client.ProductDeleteUrl(url, options)) 166 | } 167 | 168 | // ProductDeleteSignTest 商品图片搜索 - 删除 169 | func ProductDeleteSignTest(client *imagesearch.ImageSearchClient) { 170 | fmt.Println(client.ProductDeleteSign(contSign, options)) 171 | } 172 | 173 | 174 | // ***************** 绘本图片搜索 ******************** 175 | 176 | // PictureBookAddTest 绘本图片搜索 - 入库 177 | func PictureBookAddTest(client *imagesearch.ImageSearchClient) { 178 | fmt.Println(client.PictureBookAdd(image, brief, options)) 179 | } 180 | 181 | // PictureBookAddUrlTest 绘本图片搜索 - 入库 182 | func PictureBookAddUrlTest(client *imagesearch.ImageSearchClient) { 183 | fmt.Println(client.PictureBookAddUrl(url, brief, options)) 184 | } 185 | 186 | // PictureBookSearchTest 绘本图片搜索 - 检索 187 | func PictureBookSearchTest(client *imagesearch.ImageSearchClient) { 188 | fmt.Println(client.PictureBookSearch(image, options)) 189 | } 190 | 191 | // PictureBookSearchUrlTest 绘本图片搜索 - 检索 192 | func PictureBookSearchUrlTest(client *imagesearch.ImageSearchClient) { 193 | fmt.Println(client.PictureBookSearchUrl(url, options)) 194 | } 195 | 196 | // PictureBookUpdateTest 绘本图片搜索 - 更新 197 | func PictureBookUpdateTest(client *imagesearch.ImageSearchClient) { 198 | fmt.Println(client.PictureBookUpdate(image, options)) 199 | } 200 | 201 | // PictureBookUpdateUrlTest 绘本图片搜索 - 更新 202 | func PictureBookUpdateUrlTest(client *imagesearch.ImageSearchClient) { 203 | fmt.Println(client.PictureBookUpdateUrl(url, options)) 204 | } 205 | 206 | // PictureBookUpdateSignTest 绘本图片搜索 - 更新 207 | func PictureBookUpdateSignTest(client *imagesearch.ImageSearchClient) { 208 | fmt.Println(client.PictureBookUpdateSign(contSign, options)) 209 | } 210 | 211 | // PictureBookDeleteTest 绘本图片搜索 - 删除 212 | func PictureBookDeleteTest(client *imagesearch.ImageSearchClient) { 213 | fmt.Println(client.PictureBookDelete(image, options)) 214 | } 215 | 216 | // PictureBookDeleteUrlTest 绘本图片搜索 - 删除 217 | func PictureBookDeleteUrlTest(client *imagesearch.ImageSearchClient) { 218 | fmt.Println(client.PictureBookDeleteUrl(url, options)) 219 | } 220 | 221 | // PictureBookDeleteSignTest 绘本图片搜索 - 删除 222 | func PictureBookDeleteSignTest(client *imagesearch.ImageSearchClient) { 223 | fmt.Println(client.PictureBookDeleteSign(contSign, options)) 224 | } 225 | 226 | 227 | func main() { 228 | 229 | var client = imagesearch.NewClient(resources.Ak, resources.SK) 230 | 231 | SimilarAddTest(client) 232 | SimilarAddUrlTest(client) 233 | SimilarSearchTest(client) 234 | SimilarSearchUrlTest(client) 235 | SimilarUpdateTest(client) 236 | SimilarUpdateUrlTest(client) 237 | SimilarUpdateSignTest(client) 238 | SimilarDeleteTest(client) 239 | SimilarDeleteUrlTest(client) 240 | SimilarDeleteSignTest(client) 241 | 242 | 243 | SameHqAddTest(client) 244 | SameHqAddUrlTest(client) 245 | SameHqSearchTest(client) 246 | SameHqSearchUrlTest(client) 247 | SameHqUpdateTest(client) 248 | SameHqUpdateUrlTest(client) 249 | SameHqUpdateSignTest(client) 250 | SameHqDeleteTest(client) 251 | SameHqDeleteUrlTest(client) 252 | SameHqDeleteSignTest(client) 253 | 254 | 255 | 256 | ProductAddTest(client) 257 | ProductAddUrlTest(client) 258 | ProductSearchTest(client) 259 | ProductSearchUrlTest(client) 260 | ProductUpdateTest(client) 261 | ProductUpdateUrlTest(client) 262 | ProductUpdateSignTest(client) 263 | ProductDeleteTest(client) 264 | ProductDeleteUrlTest(client) 265 | ProductDeleteSignTest(client) 266 | 267 | 268 | PictureBookAddTest(client) 269 | PictureBookAddUrlTest(client) 270 | PictureBookSearchTest(client) 271 | PictureBookSearchUrlTest(client) 272 | PictureBookUpdateTest(client) 273 | PictureBookUpdateUrlTest(client) 274 | PictureBookUpdateSignTest(client) 275 | PictureBookDeleteTest(client) 276 | PictureBookDeleteUrlTest(client) 277 | PictureBookDeleteSignTest(client) 278 | 279 | } 280 | 281 | -------------------------------------------------------------------------------- /test/resources/image/baidu_image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Baidu-AIP/golang-sdk/6fa9c4b291ecafdad7dcf4683b7abdf3f741e9b9/test/resources/image/baidu_image.png -------------------------------------------------------------------------------- /util/FileUtil.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2021 baidu 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package util 18 | 19 | import ( 20 | "encoding/base64" 21 | "fmt" 22 | "io/ioutil" 23 | ) 24 | 25 | func ReadFile(path string) string { 26 | f, err := ioutil.ReadFile(path) 27 | if err != nil { 28 | fmt.Println("read fail", err) 29 | } 30 | return string(f) 31 | } 32 | 33 | func ReadFileToBase64(path string) string { 34 | data := ReadFile(path) 35 | sEnc := base64.StdEncoding.EncodeToString([]byte(data)) 36 | return sEnc 37 | } 38 | --------------------------------------------------------------------------------