aliyun_translate.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. alimt20181012 "github.com/alibabacloud-go/alimt-20181012/v2/client"
  6. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  7. util "github.com/alibabacloud-go/tea-utils/v2/service"
  8. "github.com/alibabacloud-go/tea/tea"
  9. )
  10. /**
  11. * 使用STS鉴权方式初始化账号Client,推荐此方式。本示例默认使用AK&SK方式。
  12. * @param accessKeyId
  13. * @param accessKeySecret
  14. * @param securityToken
  15. * @return Client
  16. * @throws Exception
  17. */
  18. func CreateClientWithSTS (accessKeyId *string, accessKeySecret *string, securityToken *string) (_result *alimt20181012.Client, _err error) {
  19. config := &openapi.Config{
  20. // 必填,您的 AccessKey ID
  21. AccessKeyId: accessKeyId,
  22. // 必填,您的 AccessKey Secret
  23. AccessKeySecret: accessKeySecret,
  24. // 必填,您的 Security Token
  25. SecurityToken: securityToken,
  26. // 必填,表明使用 STS 方式
  27. Type: tea.String("sts"),
  28. }
  29. // 访问的域名
  30. config.Endpoint = tea.String("mt.cn-hangzhou.aliyuncs.com")
  31. _result = &alimt20181012.Client{}
  32. _result, _err = alimt20181012.NewClient(config)
  33. return _result, _err
  34. }
  35. func handler (accessKeyId, accessKeySecret, stsToken *string, content string) (resp *alimt20181012.TranslateGeneralResponseBody, _err error) {
  36. // 初始化 Client,采用 AK&SK 鉴权访问的方式,此方式可能会存在泄漏风险,建议使用 STS 方式。鉴权访问方式请参考:https://help.aliyun.com/document_detail/378661.html
  37. //client, _err := CreateClient(accessKeyId, accessKeySecret)
  38. client, _err := CreateClientWithSTS(accessKeyId, accessKeySecret, stsToken)
  39. if _err != nil {
  40. _err = errors.New(fmt.Sprintf("创建翻译客户端失败 err: %v", _err))
  41. return
  42. }
  43. translateGeneralRequest := &alimt20181012.TranslateGeneralRequest{
  44. FormatType: tea.String("text"),
  45. SourceLanguage: tea.String("zh"),
  46. TargetLanguage: tea.String("en"),
  47. SourceText: tea.String(content),
  48. Scene: tea.String("general"),
  49. }
  50. runtime := &util.RuntimeOptions{}
  51. ret, tryErr := func()(ret *alimt20181012.TranslateGeneralResponse, _e error) {
  52. defer func() {
  53. if r := tea.Recover(recover()); r != nil {
  54. _e = r
  55. return
  56. }
  57. }()
  58. // 复制代码运行请自行打印 API 的返回值
  59. ret, _e = client.TranslateGeneralWithOptions(translateGeneralRequest, runtime)
  60. if _e != nil {
  61. return
  62. }
  63. return
  64. }()
  65. if tryErr != nil {
  66. var e = &tea.SDKError{}
  67. if _t, ok := tryErr.(*tea.SDKError); ok {
  68. e = _t
  69. } else {
  70. e.Message = tea.String(tryErr.Error())
  71. }
  72. // 如有需要,请打印 e
  73. _, _err = util.AssertAsString(e.Message)
  74. if _err != nil {
  75. _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
  76. return
  77. }
  78. _err = errors.New(fmt.Sprintf("翻译失败 err: %v", _err))
  79. return
  80. }
  81. if *ret.StatusCode != 200 {
  82. _err = errors.New(fmt.Sprintf(" %v", ret.StatusCode))
  83. return
  84. }
  85. resp = ret.Body
  86. return
  87. }
  88. func AliTranslate(content string) (contentEn string, err error) {
  89. stsToken, err := GetOssSTSToken()
  90. if err != nil {
  91. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
  92. return
  93. }
  94. fmt.Println(stsToken)
  95. resp, err := handler(&stsToken.AccessKeyId, &stsToken.AccessKeySecret, &stsToken.SecurityToken, content)
  96. if err != nil {
  97. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 err %v", err))
  98. return
  99. }
  100. if *resp.Code != 200 {
  101. err = errors.New(fmt.Sprintf("阿里云机器翻译失败 code: %v; msg: %v",*resp.Code, *resp.Message))
  102. return
  103. }
  104. contentEn = *resp.Data.Translated
  105. return
  106. }