osm_sdk.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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, configNamespace, configName, configBaseUrl string) error {
  26. prefix = configPrefix
  27. namespace = configNamespace
  28. name = configName
  29. baseUrl = configBaseUrl + servicePrefix
  30. if httpClient == nil {
  31. httpClient = http_client.New()
  32. }
  33. return nil
  34. }
  35. func GetUrl(objName string) (string, error) {
  36. return getUrl(objName)
  37. }
  38. func GetUrlWithPrefix(objName string) (string, error) {
  39. return getUrl(prefix + objName)
  40. }
  41. func getUrl(objName string) (string, error) {
  42. if strutils.IsStringEmpty(objName) {
  43. return "", nil
  44. }
  45. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  46. Get(baseUrl+"/operation/url/get",
  47. http_client.WithRequestQueryParams(map[string]string{
  48. "namespace": namespace,
  49. "name": name,
  50. "objName": objName,
  51. }))
  52. if err != nil {
  53. return "", err
  54. }
  55. resp := new(response.InfoResponse[string])
  56. err = getResponse.Json(resp)
  57. if err != nil {
  58. return "", err
  59. }
  60. if !resp.Success {
  61. return "", errors.New(resp.Msg)
  62. }
  63. return resp.Info, nil
  64. }
  65. //func ZoomUrl(obj, s string) (string, error) {
  66. // if strutils.HasBlank(obj) {
  67. // log.Println("oss发现违规参数")
  68. // return "", nil
  69. // }
  70. // return Bkt.SignURL(prefix+obj, oss.HTTPGet, 60, oss.Process(s))
  71. //}
  72. //
  73. //func ZoomUrlPfx(obj, s string, addPfx bool) (string, error) {
  74. // if strutils.HasBlank(obj) {
  75. // log.Println("oss发现违规参数")
  76. // return "", nil
  77. // }
  78. //
  79. // if addPfx {
  80. // return Bkt.SignURL(prefix+obj, oss.HTTPGet, 60, oss.Process(s))
  81. // }
  82. //
  83. // return Bkt.SignURL(obj, oss.HTTPGet, 60, oss.Process(s))
  84. //
  85. //}
  86. func ZoomUrl(objName, profess string) (string, error) {
  87. return zoomUrl(objName, profess)
  88. }
  89. func ZoomUrlWithPrefix(objName, profess string) (string, error) {
  90. return zoomUrl(prefix+objName, profess)
  91. }
  92. func zoomUrl(objName, process string) (string, error) {
  93. if strutils.IsStringEmpty(objName) {
  94. return "", nil
  95. }
  96. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  97. Post(baseUrl+"/operation/obj/zoomUrl", ZoomUrlJsonBody{
  98. ConfigKey: ConfigKey{
  99. Namespace: namespace,
  100. Name: name,
  101. },
  102. ObjName: objName,
  103. Process: process,
  104. RequireInfos: nil,
  105. })
  106. if err != nil {
  107. return "", err
  108. }
  109. resp := new(response.InfoResponse[string])
  110. err = postResponse.Json(resp)
  111. if err != nil {
  112. return "", err
  113. }
  114. if !resp.Success {
  115. return "", errors.New(resp.Msg)
  116. }
  117. return resp.Info, nil
  118. }
  119. //func Exist(obj string) (bool, error) {
  120. // if strutils.HasBlank(obj) {
  121. // return false, nil
  122. // }
  123. //
  124. // return Bkt.IsObjectExist(prefix + obj)
  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 _, objName := range objNames {
  158. objName = prefix + objName
  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 Mv(f, t string) error {
  250. // if err := Cp(f, t); err != nil {
  251. // return err
  252. // }
  253. //
  254. // return Bkt.DeleteObject(prefix + f)
  255. //}
  256. func Move(srcObjName, dstObjName string) (string, error) {
  257. return dstObjName, move(srcObjName, dstObjName, nil)
  258. }
  259. func MoveWithPrefix(srcObjName, dstObjName string) (string, error) {
  260. return prefix + dstObjName, move(prefix+srcObjName, prefix+dstObjName, nil)
  261. }
  262. func move(srcObjName, dstObjName string, requireInfos []string) error {
  263. if strutils.IsStringEmpty(srcObjName) {
  264. return nil
  265. }
  266. if strutils.IsStringEmpty(dstObjName) {
  267. return nil
  268. }
  269. putResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  270. Put(baseUrl+"/operation/obj/move", MoveJsonBody{
  271. ConfigKey: ConfigKey{
  272. Namespace: namespace,
  273. Name: name,
  274. },
  275. SrcObjName: srcObjName,
  276. DstObjName: dstObjName,
  277. RequireInfos: requireInfos,
  278. Prefix: prefix,
  279. })
  280. if err != nil {
  281. return err
  282. }
  283. resp := new(response.InfoResponse[any])
  284. err = putResponse.Json(resp)
  285. if err != nil {
  286. return err
  287. }
  288. if !resp.Success {
  289. return errors.New(resp.Msg)
  290. }
  291. return nil
  292. }
  293. //func CpOrigin(f, t string) error {
  294. // _, err := Bkt.CopyObject(f, t)
  295. // return err
  296. //}
  297. func Copy(srcObjName, dstObjName string) (string, error) {
  298. return dstObjName, copying(srcObjName, dstObjName, nil)
  299. }
  300. func CopyWithPrefix(srcObjName, dstObjName string) (string, error) {
  301. return prefix + dstObjName, copying(prefix+srcObjName, prefix+dstObjName, nil)
  302. }
  303. func copying(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. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  311. Post(baseUrl+"/operation/obj/copy", 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 CopyPublic(srcObjName, dstObjName string) (string, error) {
  335. return dstObjName, copyPublic(srcObjName, dstObjName, nil)
  336. }
  337. func CopyPublicWithPrefix(srcObjName, dstObjName string) (string, error) {
  338. return prefix + dstObjName, copyPublic(prefix+srcObjName, prefix+dstObjName, nil)
  339. }
  340. func copyPublic(srcObjName, dstObjName string, requireInfos []string) error {
  341. if strutils.IsStringEmpty(srcObjName) {
  342. return nil
  343. }
  344. if strutils.IsStringEmpty(dstObjName) {
  345. return nil
  346. }
  347. postRequest := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration))
  348. postResponse, err := postRequest.Post(baseUrl+"/operation/obj/copyPublic", CopyJsonBody{
  349. ConfigKey: ConfigKey{
  350. Namespace: namespace,
  351. Name: name,
  352. },
  353. SrcObjName: srcObjName,
  354. DstObjName: dstObjName,
  355. RequireInfos: requireInfos,
  356. Prefix: prefix,
  357. })
  358. if err != nil {
  359. return err
  360. }
  361. resp := new(response.InfoResponse[any])
  362. err = postResponse.Json(resp)
  363. if err != nil {
  364. return err
  365. }
  366. if !resp.Success {
  367. return errors.New(resp.Msg)
  368. }
  369. return nil
  370. }
  371. func CreateImage(reader io.Reader, objName string) (string, error) {
  372. return createImage(reader, objName)
  373. }
  374. func CreateImageWithPrefix(reader io.Reader, objName string) (string, error) {
  375. return createImage(reader, prefix+objName)
  376. }
  377. func createImage(reader io.Reader, objName string) (string, error) {
  378. if strutils.IsStringEmpty(objName) {
  379. return "", nil
  380. }
  381. postResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  382. Post(baseUrl+"/operation/obj/create/image", nil,
  383. http_client.WithRequestFileReader("file", objName, reader),
  384. http_client.WithRequestFormData(map[string]string{
  385. "namespace": namespace,
  386. "name": name,
  387. "objName": objName,
  388. }))
  389. if err != nil {
  390. return "", err
  391. }
  392. resp := new(response.InfoResponse[string])
  393. err = postResponse.Json(resp)
  394. if err != nil {
  395. return "", err
  396. }
  397. if !resp.Success {
  398. return "", errors.New(resp.Msg)
  399. }
  400. return resp.Info, nil
  401. }
  402. func GetUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  403. return getUrlWithMetaData(objName)
  404. }
  405. func GetUrlUrlWithMetaDataWithPrefix(objName string) (UrlWithMetaData, error) {
  406. return getUrlWithMetaData(prefix + objName)
  407. }
  408. func getUrlWithMetaData(objName string) (UrlWithMetaData, error) {
  409. var errResponse UrlWithMetaData
  410. if strutils.IsStringEmpty(objName) {
  411. return errResponse, nil
  412. }
  413. getResponse, err := httpClient.NewRequest(http_client.WithNewRequestTimeout(timeoutDuration)).
  414. Get(baseUrl+"/operation/url/metaData/get",
  415. http_client.WithRequestQueryParams(map[string]string{
  416. "namespace": namespace,
  417. "name": name,
  418. "objName": objName,
  419. }))
  420. if err != nil {
  421. return errResponse, err
  422. }
  423. resp := new(response.InfoResponse[UrlWithMetaData])
  424. err = getResponse.Json(resp)
  425. if err != nil {
  426. return errResponse, err
  427. }
  428. if !resp.Success {
  429. return errResponse, errors.New(resp.Msg)
  430. }
  431. return resp.Info, nil
  432. }
  433. //func Cp(f, t string) error {
  434. // return CpOrigin(prefix+f, prefix+t)
  435. //}
  436. //
  437. //func TouchFormatImg(reader io.Reader, to string) error {
  438. // if reader == nil || strutils.HasBlank(to) {
  439. // log.Println("oss上传发现违规参数")
  440. // return nil
  441. // }
  442. //
  443. // return Bkt.PutObject(prefix+to, reader, oss.ContentType("image/jpg"))
  444. //}
  445. //func GetObjectBytes(obj string) ([]byte, error) {
  446. // if strutils.HasBlank(obj) {
  447. // return nil, errors.New("参数错误")
  448. // }
  449. // buf := new(bytes.Buffer)
  450. // body, err := Bkt.GetObject(prefix + obj)
  451. // if err != nil {
  452. // return nil, err
  453. // }
  454. // _, err = io.Copy(buf, body)
  455. // if err != nil {
  456. // return nil, err
  457. // }
  458. // err = body.Close()
  459. // if err != nil {
  460. // return nil, err
  461. // }
  462. //
  463. // return buf.Bytes(), nil
  464. //}
  465. //func getObjectBytes(objName, userId string) ([]byte, error) {
  466. // if strutils.IsStringEmpty(objName) {
  467. // return nil, nil
  468. // }
  469. // if strutils.IsStringEmpty(objName) {
  470. // return nil, nil
  471. // }
  472. // buf := new(bytes.Buffer)
  473. //
  474. // responseBytes, err := httpClient.GetWithoutHeaders(baseUrl+"/operation/obj/getContent", map[string]string{
  475. // "namespace": namespace,
  476. // "name": name,
  477. // "objName": objName,
  478. // "userId": userId,
  479. // })
  480. // if err != nil {
  481. // return nil, err
  482. // }
  483. //
  484. // resp := new(response.InfoResponse[[]byte])
  485. // err = json.Unmarshal(responseBytes, resp)
  486. // if err != nil {
  487. // return nil, err
  488. // }
  489. //
  490. // if !resp.Success {
  491. // return nil, errors.New(resp.Msg)
  492. // }
  493. //
  494. // //return resp.Info, nil
  495. // _, err = io.Copy(buf, resp.Info)
  496. // if err != nil {
  497. // return nil, err
  498. // }
  499. // err = body.Close()
  500. // if err != nil {
  501. // return nil, err
  502. // }
  503. //
  504. // return buf.Bytes(), nil
  505. //}
  506. //func MvWithoutPrefix(f, t string) error {
  507. // if _, err := Bkt.CopyObject(f, t); err != nil {
  508. // return err
  509. // }
  510. //
  511. // return Bkt.DeleteObject(f)
  512. //}