osm_sdk.go 14 KB

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