osm_sdk.go 13 KB

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