osm_sdk.go 13 KB

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