osm_sdk.go 13 KB

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