osm_sdk.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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(obj, s string) (string, error) {
  69. // if strutils.HasBlank(obj) {
  70. // log.Println("oss发现违规参数")
  71. // return "", nil
  72. // }
  73. // return Bkt.SignURL(prefix+obj, oss.HTTPGet, 60, oss.Process(s))
  74. //}
  75. //
  76. //func ZoomUrlPfx(obj, s string, addPfx bool) (string, error) {
  77. // if strutils.HasBlank(obj) {
  78. // log.Println("oss发现违规参数")
  79. // return "", nil
  80. // }
  81. //
  82. // if addPfx {
  83. // return Bkt.SignURL(prefix+obj, oss.HTTPGet, 60, oss.Process(s))
  84. // }
  85. //
  86. // return Bkt.SignURL(obj, oss.HTTPGet, 60, oss.Process(s))
  87. //
  88. //}
  89. func ZoomUrl(objName, profess string) (string, error) {
  90. return zoomUrl(objName, profess)
  91. }
  92. func ZoomUrlWithPrefix(objName, profess string) (string, error) {
  93. return zoomUrl(prefix+objName, profess)
  94. }
  95. func zoomUrl(objName, process string) (string, error) {
  96. if strutils.IsStringEmpty(objName) {
  97. return "", nil
  98. }
  99. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  100. Post(baseUrl+"/operation/obj/zoomUrl", ZoomUrlJsonBody{
  101. ConfigKey: ConfigKey{
  102. Namespace: namespace,
  103. Name: name,
  104. },
  105. ObjName: objName,
  106. Process: process,
  107. RequireInfos: nil,
  108. })
  109. if err != nil {
  110. return "", err
  111. }
  112. resp := new(response.InfoResponse[string])
  113. err = postResponse.Json(resp)
  114. if err != nil {
  115. return "", err
  116. }
  117. if !resp.Success {
  118. return "", errors.New(resp.Msg)
  119. }
  120. return resp.Info, nil
  121. }
  122. //func Exist(obj string) (bool, error) {
  123. // if strutils.HasBlank(obj) {
  124. // return false, nil
  125. // }
  126. //
  127. // return Bkt.IsObjectExist(prefix + obj)
  128. //}
  129. func Exist(objName string) (bool, error) {
  130. return exist(objName)
  131. }
  132. func ExistWithPrefix(objName string) (bool, error) {
  133. return exist(prefix + objName)
  134. }
  135. func exist(objName string) (bool, error) {
  136. if strutils.IsStringEmpty(objName) {
  137. return false, nil
  138. }
  139. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/obj/check", http_client.WithRequestQueryParams(map[string]string{
  140. "namespace": namespace,
  141. "name": name,
  142. "objName": objName}))
  143. if err != nil {
  144. return false, err
  145. }
  146. resp := new(response.InfoResponse[bool])
  147. err = getResponse.Json(resp)
  148. if err != nil {
  149. return false, err
  150. }
  151. if !resp.Success {
  152. return false, errors.New(resp.Msg)
  153. }
  154. return true, nil
  155. }
  156. func DeleteMulti(objNames ...string) error {
  157. return deleteMulti(objNames...)
  158. }
  159. func DeleteMultiWithPrefix(userId string, objNames ...string) error {
  160. for i := range objNames {
  161. objNames[i] = prefix + objNames[i]
  162. }
  163. return deleteMulti(objNames...)
  164. }
  165. func deleteMulti(objNames ...string) error {
  166. if len(objNames) == 0 {
  167. return nil
  168. }
  169. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Post(baseUrl+"/operation/obj/deleteMulti", DeleteMultiObjJsonBody{
  170. ConfigKey: ConfigKey{
  171. Namespace: namespace,
  172. Name: name,
  173. },
  174. Prefix: prefix,
  175. ObjNames: objNames,
  176. })
  177. if err != nil {
  178. return err
  179. }
  180. resp := new(response.MsgResponse)
  181. err = postResponse.Json(resp)
  182. if err != nil {
  183. return err
  184. }
  185. if !resp.Success {
  186. return errors.New(resp.Msg)
  187. }
  188. return nil
  189. }
  190. func ListDir(dirPath string) ([]string, error) {
  191. return listDir(dirPath)
  192. }
  193. func ListDirWithPrefix(dirPath string) ([]string, error) {
  194. return listDir(prefix + dirPath)
  195. }
  196. func listDir(dirPath string) ([]string, error) {
  197. if strutils.IsStringEmpty(dirPath) {
  198. return nil, nil
  199. }
  200. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).Get(baseUrl+"/operation/objPath/list", http_client.WithRequestQueryParams(map[string]string{
  201. "namespace": namespace,
  202. "name": name,
  203. "dirPath": dirPath,
  204. }))
  205. if err != nil {
  206. return nil, err
  207. }
  208. resp := new(response.InfoResponse[[]string])
  209. err = getResponse.Json(resp)
  210. if err != nil {
  211. return nil, err
  212. }
  213. if !resp.Success {
  214. return nil, errors.New(resp.Msg)
  215. }
  216. return resp.Info, nil
  217. }
  218. func LsDetails(dirPath string) ([]ObjectInfo, error) {
  219. return lsDetails(dirPath)
  220. }
  221. func LsDetailsWithPrefix(dirPath string) ([]ObjectInfo, error) {
  222. return lsDetails(prefix + dirPath)
  223. }
  224. type ObjectInfo struct {
  225. Key string
  226. Type string
  227. Size int64
  228. }
  229. func lsDetails(dirPath string) ([]ObjectInfo, error) {
  230. if strutils.IsStringEmpty(dirPath) {
  231. return []ObjectInfo{}, nil
  232. }
  233. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  234. Get(baseUrl+"/operation/objPath/listDetail", http_client.WithRequestQueryParams(map[string]string{
  235. "namespace": namespace,
  236. "name": name,
  237. "dirPath": dirPath,
  238. }))
  239. if err != nil {
  240. return nil, err
  241. }
  242. resp := new(response.InfoResponse[[]ObjectInfo])
  243. err = getResponse.Json(resp)
  244. if err != nil {
  245. return nil, err
  246. }
  247. if !resp.Success {
  248. return nil, errors.New(resp.Msg)
  249. }
  250. return resp.Info, nil
  251. }
  252. //func Mv(f, t string) error {
  253. // if err := Cp(f, t); err != nil {
  254. // return err
  255. // }
  256. //
  257. // return Bkt.DeleteObject(prefix + f)
  258. //}
  259. func Move(srcObjName, dstObjName string) (string, error) {
  260. return dstObjName, move(srcObjName, dstObjName, nil)
  261. }
  262. func MoveWithPrefix(srcObjName, dstObjName string) (string, error) {
  263. return prefix + dstObjName, move(prefix+srcObjName, prefix+dstObjName, nil)
  264. }
  265. func move(srcObjName, dstObjName string, requireInfos []string) error {
  266. if strutils.IsStringEmpty(srcObjName) {
  267. return nil
  268. }
  269. if strutils.IsStringEmpty(dstObjName) {
  270. return nil
  271. }
  272. putResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  273. Put(baseUrl+"/operation/obj/move", MoveJsonBody{
  274. ConfigKey: ConfigKey{
  275. Namespace: namespace,
  276. Name: name,
  277. },
  278. SrcObjName: srcObjName,
  279. DstObjName: dstObjName,
  280. RequireInfos: requireInfos,
  281. Prefix: prefix,
  282. })
  283. if err != nil {
  284. return err
  285. }
  286. resp := new(response.InfoResponse[any])
  287. err = putResponse.Json(resp)
  288. if err != nil {
  289. return err
  290. }
  291. if !resp.Success {
  292. return errors.New(resp.Msg)
  293. }
  294. return nil
  295. }
  296. //func CpOrigin(f, t string) error {
  297. // _, err := Bkt.CopyObject(f, t)
  298. // return err
  299. //}
  300. func Copy(srcObjName, dstObjName string) (string, error) {
  301. return dstObjName, copying(srcObjName, dstObjName, nil)
  302. }
  303. func CopyWithPrefix(srcObjName, dstObjName string) (string, error) {
  304. return prefix + dstObjName, copying(prefix+srcObjName, prefix+dstObjName, nil)
  305. }
  306. func copying(srcObjName, dstObjName string, requireInfos []string) error {
  307. if strutils.IsStringEmpty(srcObjName) {
  308. return nil
  309. }
  310. if strutils.IsStringEmpty(dstObjName) {
  311. return nil
  312. }
  313. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  314. Post(baseUrl+"/operation/obj/copy", CopyJsonBody{
  315. ConfigKey: ConfigKey{
  316. Namespace: namespace,
  317. Name: name,
  318. },
  319. SrcObjName: srcObjName,
  320. DstObjName: dstObjName,
  321. RequireInfos: requireInfos,
  322. Prefix: prefix,
  323. })
  324. if err != nil {
  325. return err
  326. }
  327. resp := new(response.InfoResponse[any])
  328. err = postResponse.Json(resp)
  329. if err != nil {
  330. return err
  331. }
  332. if !resp.Success {
  333. return errors.New(resp.Msg)
  334. }
  335. return nil
  336. }
  337. func CopyPublic(srcObjName, dstObjName string) (string, error) {
  338. return dstObjName, copyPublic(srcObjName, dstObjName, nil)
  339. }
  340. func CopyPublicWithPrefix(srcObjName, dstObjName string) (string, error) {
  341. return prefix + dstObjName, copyPublic(prefix+srcObjName, prefix+dstObjName, nil)
  342. }
  343. func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
  344. if strutils.IsStringEmpty(srcObjName) {
  345. return nil
  346. }
  347. if strutils.IsStringEmpty(dstObjName) {
  348. return nil
  349. }
  350. postRequest := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration))
  351. postResponse, err := postRequest.Post(baseUrl+"/operation/obj/copyPublic", CopyJsonBody{
  352. ConfigKey: ConfigKey{
  353. Namespace: namespace,
  354. Name: name,
  355. },
  356. SrcObjName: srcObjName,
  357. DstObjName: dstObjName,
  358. RequireInfos: requireInfos,
  359. Prefix: prefix,
  360. })
  361. if err != nil {
  362. return err
  363. }
  364. resp := new(response.InfoResponse[any])
  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 nil
  373. }
  374. func CreateImage(reader io.Reader, objName string) (string, error) {
  375. return createImage(reader, objName)
  376. }
  377. func CreateImageWithPrefix(reader io.Reader, objName string) (string, error) {
  378. return createImage(reader, prefix+objName)
  379. }
  380. func createImage(reader io.Reader, objName 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/image", 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. }))
  392. if err != nil {
  393. return "", err
  394. }
  395. resp := new(response.InfoResponse[string])
  396. err = postResponse.Json(resp)
  397. if err != nil {
  398. return "", err
  399. }
  400. if !resp.Success {
  401. return "", errors.New(resp.Msg)
  402. }
  403. return resp.Info, nil
  404. }
  405. func CreateFile(reader io.Reader, objName string, contentType string) (string, error) {
  406. return createFile(reader, objName, contentType)
  407. }
  408. func CreateFileWithPrefix(reader io.Reader, objName string, contentType string) (string, error) {
  409. return createFile(reader, prefix+objName, contentType)
  410. }
  411. func createFile(reader io.Reader, objName string, contentType string) (string, error) {
  412. if strutils.IsStringEmpty(objName) {
  413. return "", nil
  414. }
  415. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  416. Post(baseUrl+"/operation/obj/create/file", nil,
  417. http_client.WithRequestFileReader("file", objName, reader),
  418. http_client.WithRequestFormData(map[string]string{
  419. "namespace": namespace,
  420. "name": name,
  421. "objName": objName,
  422. "contentType": contentType,
  423. }))
  424. if err != nil {
  425. return "", err
  426. }
  427. resp := new(response.InfoResponse[string])
  428. err = postResponse.Json(resp)
  429. if err != nil {
  430. return "", err
  431. }
  432. if !resp.Success {
  433. return "", errors.New(resp.Msg)
  434. }
  435. return resp.Info, nil
  436. }
  437. func GetUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  438. return getUrlWithMetaData(objName)
  439. }
  440. func GetUrlUrlWithMetaDataWithPrefix(objName string) (UrlWithMetaData, error) {
  441. return getUrlWithMetaData(prefix + objName)
  442. }
  443. func getUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  444. var errResponse UrlWithMetaData
  445. if strutils.IsStringEmpty(objName) {
  446. return errResponse, nil
  447. }
  448. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  449. Get(baseUrl+"/operation/url/metaData/get",
  450. http_client.WithRequestQueryParams(map[string]string{
  451. "namespace": namespace,
  452. "name": name,
  453. "objName": objName,
  454. }))
  455. if err != nil {
  456. return errResponse, err
  457. }
  458. resp := new(response.InfoResponse[UrlWithMetaData])
  459. err = getResponse.Json(resp)
  460. if err != nil {
  461. return errResponse, err
  462. }
  463. if !resp.Success {
  464. return errResponse, errors.New(resp.Msg)
  465. }
  466. return resp.Info, nil
  467. }
  468. //func Cp(f, t string) error {
  469. // return CpOrigin(prefix+f, prefix+t)
  470. //}
  471. //
  472. //func TouchFormatImg(reader io.Reader, to string) error {
  473. // if reader == nil || strutils.HasBlank(to) {
  474. // log.Println("oss上传发现违规参数")
  475. // return nil
  476. // }
  477. //
  478. // return Bkt.PutObject(prefix+to, reader, oss.ContentType("image/jpg"))
  479. //}
  480. //func GetObjectBytes(obj string) ([]byte, error) {
  481. // if strutils.HasBlank(obj) {
  482. // return nil, errors.New("参数错误")
  483. // }
  484. // buf := new(bytes.Buffer)
  485. // body, err := Bkt.GetObject(prefix + obj)
  486. // if err != nil {
  487. // return nil, err
  488. // }
  489. // _, err = io.Copy(buf, body)
  490. // if err != nil {
  491. // return nil, err
  492. // }
  493. // err = body.Close()
  494. // if err != nil {
  495. // return nil, err
  496. // }
  497. //
  498. // return buf.Bytes(), nil
  499. //}
  500. //func getObjectBytes(objName, userId string) ([]byte, error) {
  501. // if strutils.IsStringEmpty(objName) {
  502. // return nil, nil
  503. // }
  504. // if strutils.IsStringEmpty(objName) {
  505. // return nil, nil
  506. // }
  507. // buf := new(bytes.Buffer)
  508. //
  509. // responseBytes, err := httpClient.GetWithoutHeaders(baseUrl+"/operation/obj/getContent", map[string]string{
  510. // "namespace": namespace,
  511. // "name": name,
  512. // "objName": objName,
  513. // "userId": userId,
  514. // })
  515. // if err != nil {
  516. // return nil, err
  517. // }
  518. //
  519. // resp := new(response.InfoResponse[[]byte])
  520. // err = json.Unmarshal(responseBytes, resp)
  521. // if err != nil {
  522. // return nil, err
  523. // }
  524. //
  525. // if !resp.Success {
  526. // return nil, errors.New(resp.Msg)
  527. // }
  528. //
  529. // //return resp.Info, nil
  530. // _, err = io.Copy(buf, resp.Info)
  531. // if err != nil {
  532. // return nil, err
  533. // }
  534. // err = body.Close()
  535. // if err != nil {
  536. // return nil, err
  537. // }
  538. //
  539. // return buf.Bytes(), nil
  540. //}
  541. //func MvWithoutPrefix(f, t string) error {
  542. // if _, err := Bkt.CopyObject(f, t); err != nil {
  543. // return err
  544. // }
  545. //
  546. // return Bkt.DeleteObject(f)
  547. //}