osm_sdk.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. package osm_sdk
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  4. "git.sxidc.com/go-tools/utils/http_client"
  5. "git.sxidc.com/go-tools/utils/strutils"
  6. "github.com/pkg/errors"
  7. "io"
  8. "time"
  9. )
  10. const TimeoutSec = 30
  11. var timeoutDuration = TimeoutSec * time.Second
  12. const servicePrefix = "/osm/api/v1"
  13. var prefix string
  14. var namespace string
  15. var name string
  16. var baseUrl string
  17. var httpClient *http_client.Client
  18. func Destroy() {
  19. if httpClient == nil {
  20. return
  21. }
  22. http_client.Destroy(httpClient)
  23. httpClient = nil
  24. }
  25. func Init(configPrefix string, configNamespace string, configName string, configBaseUrl string, duration time.Duration) error {
  26. if duration != 0 {
  27. timeoutDuration = duration
  28. }
  29. if strutils.IsStringEmpty(configNamespace) {
  30. return errors.New("namespace is empty")
  31. }
  32. if strutils.IsStringEmpty(configName) {
  33. return errors.New("name is empty")
  34. }
  35. if strutils.IsStringEmpty(configBaseUrl) {
  36. return errors.New("baseUrl is empty")
  37. }
  38. prefix = configPrefix
  39. namespace = configNamespace
  40. name = configName
  41. baseUrl = configBaseUrl + servicePrefix
  42. if httpClient == nil {
  43. httpClient = http_client.New()
  44. }
  45. return nil
  46. }
  47. func GetUrl(objName string) (string, error) {
  48. return getUrl(objName)
  49. }
  50. func GetUrlWithPrefix(objName string) (string, error) {
  51. return getUrl(prefix + objName)
  52. }
  53. func getUrl(objName string) (string, error) {
  54. if strutils.IsStringEmpty(objName) {
  55. return "", nil
  56. }
  57. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  58. Get(baseUrl+"/operation/url/get",
  59. http_client.WithRequestQueryParams(map[string]string{
  60. "namespace": namespace,
  61. "name": name,
  62. "objName": objName,
  63. }))
  64. if err != nil {
  65. return "", err
  66. }
  67. resp := new(response.InfoResponse[string])
  68. err = getResponse.Json(resp)
  69. if err != nil {
  70. return "", err
  71. }
  72. if !resp.Success {
  73. return "", errors.New(resp.Msg)
  74. }
  75. return resp.Info, nil
  76. }
  77. func ZoomUrl(objName, profess string) (string, error) {
  78. return zoomUrl(objName, profess)
  79. }
  80. func ZoomUrlWithPrefix(objName, profess string) (string, error) {
  81. return zoomUrl(prefix+objName, profess)
  82. }
  83. func zoomUrl(objName, process string) (string, error) {
  84. if strutils.IsStringEmpty(objName) {
  85. return "", nil
  86. }
  87. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  88. Post(baseUrl+"/operation/obj/zoomUrl", ZoomUrlJsonBody{
  89. ConfigKey: ConfigKey{
  90. Namespace: namespace,
  91. Name: name,
  92. },
  93. ObjName: objName,
  94. Process: process,
  95. })
  96. if err != nil {
  97. return "", err
  98. }
  99. resp := new(response.InfoResponse[string])
  100. err = postResponse.Json(resp)
  101. if err != nil {
  102. return "", err
  103. }
  104. if !resp.Success {
  105. return "", errors.New(resp.Msg)
  106. }
  107. return resp.Info, nil
  108. }
  109. func Exist(objName string) (bool, error) {
  110. return exist(objName)
  111. }
  112. func ExistWithPrefix(objName string) (bool, error) {
  113. return exist(prefix + objName)
  114. }
  115. func exist(objName string) (bool, error) {
  116. if strutils.IsStringEmpty(objName) {
  117. return false, nil
  118. }
  119. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/obj/check", http_client.WithRequestQueryParams(map[string]string{
  120. "namespace": namespace,
  121. "name": name,
  122. "objName": objName}))
  123. if err != nil {
  124. return false, err
  125. }
  126. resp := new(response.InfoResponse[bool])
  127. err = getResponse.Json(resp)
  128. if err != nil {
  129. return false, err
  130. }
  131. if !resp.Success {
  132. return false, errors.New(resp.Msg)
  133. }
  134. return true, nil
  135. }
  136. func DeleteMulti(objNames ...string) error {
  137. return deleteMulti(objNames...)
  138. }
  139. func DeleteMultiWithPrefix(userId string, objNames ...string) error {
  140. for i := range objNames {
  141. objNames[i] = prefix + objNames[i]
  142. }
  143. return deleteMulti(objNames...)
  144. }
  145. func deleteMulti(objNames ...string) error {
  146. if len(objNames) == 0 {
  147. return nil
  148. }
  149. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Post(baseUrl+"/operation/obj/deleteMulti", DeleteMultiObjJsonBody{
  150. ConfigKey: ConfigKey{
  151. Namespace: namespace,
  152. Name: name,
  153. },
  154. Prefix: prefix,
  155. ObjNames: objNames,
  156. })
  157. if err != nil {
  158. return err
  159. }
  160. resp := new(response.MsgResponse)
  161. err = postResponse.Json(resp)
  162. if err != nil {
  163. return err
  164. }
  165. if !resp.Success {
  166. return errors.New(resp.Msg)
  167. }
  168. return nil
  169. }
  170. func ListDir(dirPath string) ([]string, error) {
  171. return listDir(dirPath)
  172. }
  173. func ListDirWithPrefix(dirPath string) ([]string, error) {
  174. return listDir(prefix + dirPath)
  175. }
  176. func listDir(dirPath string) ([]string, error) {
  177. if strutils.IsStringEmpty(dirPath) {
  178. return nil, nil
  179. }
  180. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/objPath/list", http_client.WithRequestQueryParams(map[string]string{
  181. "namespace": namespace,
  182. "name": name,
  183. "dirPath": dirPath,
  184. }))
  185. if err != nil {
  186. return nil, err
  187. }
  188. resp := new(response.InfoResponse[[]string])
  189. err = getResponse.Json(resp)
  190. if err != nil {
  191. return nil, err
  192. }
  193. if !resp.Success {
  194. return nil, errors.New(resp.Msg)
  195. }
  196. return resp.Info, nil
  197. }
  198. func LsDetails(dirPath string) ([]ObjectInfo, error) {
  199. return lsDetails(dirPath)
  200. }
  201. func LsDetailsWithPrefix(dirPath string) ([]ObjectInfo, error) {
  202. return lsDetails(prefix + dirPath)
  203. }
  204. type ObjectInfo struct {
  205. Key string
  206. Type string
  207. Size int64
  208. }
  209. func lsDetails(dirPath string) ([]ObjectInfo, error) {
  210. if strutils.IsStringEmpty(dirPath) {
  211. return []ObjectInfo{}, nil
  212. }
  213. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  214. Get(baseUrl+"/operation/objPath/listDetail", http_client.WithRequestQueryParams(map[string]string{
  215. "namespace": namespace,
  216. "name": name,
  217. "dirPath": dirPath,
  218. }))
  219. if err != nil {
  220. return nil, err
  221. }
  222. resp := new(response.InfoResponse[[]ObjectInfo])
  223. err = getResponse.Json(resp)
  224. if err != nil {
  225. return nil, err
  226. }
  227. if !resp.Success {
  228. return nil, errors.New(resp.Msg)
  229. }
  230. return resp.Info, nil
  231. }
  232. func Move(srcObjName, dstObjName string) (string, error) {
  233. return dstObjName, move(srcObjName, dstObjName, nil)
  234. }
  235. func MoveWithPrefix(srcObjName, dstObjName string) (string, error) {
  236. return prefix + dstObjName, move(prefix+srcObjName, prefix+dstObjName, nil)
  237. }
  238. func move(srcObjName, dstObjName string, requireInfos []string) error {
  239. if strutils.IsStringEmpty(srcObjName) {
  240. return nil
  241. }
  242. if strutils.IsStringEmpty(dstObjName) {
  243. return nil
  244. }
  245. putResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  246. Put(baseUrl+"/operation/obj/move", MoveJsonBody{
  247. ConfigKey: ConfigKey{
  248. Namespace: namespace,
  249. Name: name,
  250. },
  251. SrcObjName: srcObjName,
  252. DstObjName: dstObjName,
  253. RequireInfos: requireInfos,
  254. Prefix: prefix,
  255. })
  256. if err != nil {
  257. return err
  258. }
  259. resp := new(response.InfoResponse[any])
  260. err = putResponse.Json(resp)
  261. if err != nil {
  262. return err
  263. }
  264. if !resp.Success {
  265. return errors.New(resp.Msg)
  266. }
  267. return nil
  268. }
  269. func Copy(srcObjName, dstObjName string) (string, error) {
  270. return dstObjName, copying(srcObjName, dstObjName, nil)
  271. }
  272. func CopyWithPrefix(srcObjName, dstObjName string) (string, error) {
  273. return prefix + dstObjName, copying(prefix+srcObjName, prefix+dstObjName, nil)
  274. }
  275. func copying(srcObjName, dstObjName string, requireInfos []string) error {
  276. if strutils.IsStringEmpty(srcObjName) {
  277. return nil
  278. }
  279. if strutils.IsStringEmpty(dstObjName) {
  280. return nil
  281. }
  282. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  283. Post(baseUrl+"/operation/obj/copy", CopyJsonBody{
  284. ConfigKey: ConfigKey{
  285. Namespace: namespace,
  286. Name: name,
  287. },
  288. SrcObjName: srcObjName,
  289. DstObjName: dstObjName,
  290. RequireInfos: requireInfos,
  291. Prefix: prefix,
  292. })
  293. if err != nil {
  294. return err
  295. }
  296. resp := new(response.InfoResponse[any])
  297. err = postResponse.Json(resp)
  298. if err != nil {
  299. return err
  300. }
  301. if !resp.Success {
  302. return errors.New(resp.Msg)
  303. }
  304. return nil
  305. }
  306. func CopyPublic(srcObjName, dstObjName string) (string, error) {
  307. return dstObjName, copyPublic(srcObjName, dstObjName, nil)
  308. }
  309. func CopyPublicWithPrefix(srcObjName, dstObjName string) (string, error) {
  310. return prefix + dstObjName, copyPublic(prefix+srcObjName, prefix+dstObjName, nil)
  311. }
  312. func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
  313. if strutils.IsStringEmpty(srcObjName) {
  314. return nil
  315. }
  316. if strutils.IsStringEmpty(dstObjName) {
  317. return nil
  318. }
  319. postRequest := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration))
  320. postResponse, err := postRequest.Post(baseUrl+"/operation/obj/copyPublic", CopyJsonBody{
  321. ConfigKey: ConfigKey{
  322. Namespace: namespace,
  323. Name: name,
  324. },
  325. SrcObjName: srcObjName,
  326. DstObjName: dstObjName,
  327. RequireInfos: requireInfos,
  328. Prefix: prefix,
  329. })
  330. if err != nil {
  331. return err
  332. }
  333. resp := new(response.InfoResponse[any])
  334. err = postResponse.Json(resp)
  335. if err != nil {
  336. return err
  337. }
  338. if !resp.Success {
  339. return errors.New(resp.Msg)
  340. }
  341. return nil
  342. }
  343. func CreateImage(reader io.Reader, objName string) (string, error) {
  344. return createImage(reader, objName)
  345. }
  346. func CreateImageWithPrefix(reader io.Reader, objName string) (string, error) {
  347. return createImage(reader, prefix+objName)
  348. }
  349. func createImage(reader io.Reader, objName string) (string, error) {
  350. if strutils.IsStringEmpty(objName) {
  351. return "", nil
  352. }
  353. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  354. Post(baseUrl+"/operation/obj/create/image", nil,
  355. http_client.WithRequestFileReader("file", objName, reader),
  356. http_client.WithRequestFormData(map[string]string{
  357. "namespace": namespace,
  358. "name": name,
  359. "objName": objName,
  360. }))
  361. if err != nil {
  362. return "", err
  363. }
  364. resp := new(response.InfoResponse[string])
  365. err = postResponse.Json(resp)
  366. if err != nil {
  367. return "", err
  368. }
  369. if !resp.Success {
  370. return "", errors.New(resp.Msg)
  371. }
  372. return resp.Info, nil
  373. }
  374. func CreateFile(reader io.Reader, objName string, contentType string) (string, error) {
  375. return createFile(reader, objName, contentType)
  376. }
  377. func CreateFileWithPrefix(reader io.Reader, objName string, contentType string) (string, error) {
  378. return createFile(reader, prefix+objName, contentType)
  379. }
  380. func createFile(reader io.Reader, objName string, contentType string) (string, error) {
  381. if strutils.IsStringEmpty(objName) {
  382. return "", nil
  383. }
  384. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  385. Post(baseUrl+"/operation/obj/create/file", nil,
  386. http_client.WithRequestFileReader("file", objName, reader),
  387. http_client.WithRequestFormData(map[string]string{
  388. "namespace": namespace,
  389. "name": name,
  390. "objName": objName,
  391. "contentType": contentType,
  392. }))
  393. if err != nil {
  394. return "", err
  395. }
  396. resp := new(response.InfoResponse[string])
  397. err = postResponse.Json(resp)
  398. if err != nil {
  399. return "", err
  400. }
  401. if !resp.Success {
  402. return "", errors.New(resp.Msg)
  403. }
  404. return resp.Info, nil
  405. }
  406. func GetUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  407. return getUrlWithMetaData(objName)
  408. }
  409. func GetUrlUrlWithMetaDataWithPrefix(objName string) (UrlWithMetaData, error) {
  410. return getUrlWithMetaData(prefix + objName)
  411. }
  412. func getUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  413. var errResponse UrlWithMetaData
  414. if strutils.IsStringEmpty(objName) {
  415. return errResponse, nil
  416. }
  417. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  418. Get(baseUrl+"/operation/url/metaData/get",
  419. http_client.WithRequestQueryParams(map[string]string{
  420. "namespace": namespace,
  421. "name": name,
  422. "objName": objName,
  423. }))
  424. if err != nil {
  425. return errResponse, err
  426. }
  427. resp := new(response.InfoResponse[UrlWithMetaData])
  428. err = getResponse.Json(resp)
  429. if err != nil {
  430. return errResponse, err
  431. }
  432. if !resp.Success {
  433. return errResponse, errors.New(resp.Msg)
  434. }
  435. return resp.Info, nil
  436. }
  437. //func GetObjectBytes(obj string) ([]byte, error) {
  438. // if strutils.HasBlank(obj) {
  439. // return nil, errors.New("参数错误")
  440. // }
  441. // buf := new(bytes.Buffer)
  442. // body, err := Bkt.GetObject(prefix + obj)
  443. // if err != nil {
  444. // return nil, err
  445. // }
  446. // _, err = io.Copy(buf, body)
  447. // if err != nil {
  448. // return nil, err
  449. // }
  450. // err = body.Close()
  451. // if err != nil {
  452. // return nil, err
  453. // }
  454. //
  455. // return buf.Bytes(), nil
  456. //}
  457. //func getObjectBytes(objName, userId string) ([]byte, error) {
  458. // if strutils.IsStringEmpty(objName) {
  459. // return nil, nil
  460. // }
  461. // if strutils.IsStringEmpty(objName) {
  462. // return nil, nil
  463. // }
  464. // buf := new(bytes.Buffer)
  465. //
  466. // responseBytes, err := httpClient.GetWithoutHeaders(baseUrl+"/operation/obj/getContent", map[string]string{
  467. // "namespace": namespace,
  468. // "name": name,
  469. // "objName": objName,
  470. // "userId": userId,
  471. // })
  472. // if err != nil {
  473. // return nil, err
  474. // }
  475. //
  476. // resp := new(response.InfoResponse[[]byte])
  477. // err = json.Unmarshal(responseBytes, resp)
  478. // if err != nil {
  479. // return nil, err
  480. // }
  481. //
  482. // if !resp.Success {
  483. // return nil, errors.New(resp.Msg)
  484. // }
  485. //
  486. // //return resp.Info, nil
  487. // _, err = io.Copy(buf, resp.Info)
  488. // if err != nil {
  489. // return nil, err
  490. // }
  491. // err = body.Close()
  492. // if err != nil {
  493. // return nil, err
  494. // }
  495. //
  496. // return buf.Bytes(), nil
  497. //}