minio.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/minio/minio-go/v7"
  8. "github.com/minio/minio-go/v7/pkg/credentials"
  9. "log"
  10. "os"
  11. "time"
  12. )
  13. func GetMinIOSTSToken() (item *Token, err error) {
  14. // MinIO服务的访问信息
  15. item = new(Token)
  16. //useSSL := false
  17. //if utils.MinIoUseSSL == "true" {
  18. // useSSL = true
  19. //}
  20. // 创建MinIO客户端
  21. //minioClient, err := minio.New(utils.MinIoEndpoint, &minio.Options{
  22. // Creds: credentials.NewStaticV4(utils.MinIoAccessKeyId, utils.MinIoAccessKeySecret, ""),
  23. // Secure: useSSL,
  24. //})
  25. //if err != nil {
  26. // return nil, err
  27. //}
  28. // 设置STS凭证请求参数
  29. //policy := `{
  30. // "Version": "2012-10-17",
  31. // "Statement": [
  32. // {
  33. // "Sid": "",
  34. // "Effect": "Allow",
  35. // "Principal": {"AWS": "arn:aws:iam::1234567890:root"},
  36. // "Action": "s3:GetObject",
  37. // "Resource": "arn:aws:s3:::<YourBucketName>/*"
  38. // }
  39. // ]
  40. //}`
  41. //expiry := time.Hour * 24 // STS凭证的过期时间
  42. //获取STS凭证
  43. //stsCredentials, err := minioClient.PresignedPutObject(context.Background(), "etastatic", "myobject", expiry)
  44. //if err != nil {
  45. // return
  46. //}
  47. item.AccessKeyId = utils.MinIoAccessKeyId
  48. item.SecretKeyId = utils.MinIoAccessKeySecret
  49. item.Endpoint = utils.MinIoEndpoint
  50. item.ImgHost = utils.MinIoImghost
  51. item.Bucketname = utils.MinIoBucketname
  52. item.UseSSL = utils.MinIoUseSSL
  53. item.RegionId = utils.MinIoRegion
  54. item.Port = utils.MinIoPort
  55. return
  56. }
  57. type Token struct {
  58. AccessKeyId string
  59. SecretKeyId string
  60. RegionId string
  61. Bucketname string
  62. Endpoint string
  63. ImgHost string
  64. UseSSL string
  65. Port string
  66. }
  67. func UploadMinIo() {
  68. ctx := context.Background()
  69. endpoint := "8.136.199.33:9000/"
  70. accessKeyID := "LfQ8uiJiLP7vLxjRrmNW"
  71. secretAccessKey := "IszGVHsNicJMQxHC46cYFtbrOiapo0ynwOIJ6c2R"
  72. useSSL := false
  73. // Initialize minio client object.
  74. minioClient, err := minio.New(endpoint, &minio.Options{
  75. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  76. Secure: useSSL,
  77. })
  78. if err != nil {
  79. log.Fatalln(err)
  80. }
  81. // Make a new bucket called mymusic.
  82. bucketName := "etastatic"
  83. location := "/"
  84. err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
  85. if err != nil {
  86. // Check to see if we already own this bucket (which happens if you run this twice)
  87. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  88. if errBucketExists == nil && exists {
  89. log.Printf("We already own %s\n", bucketName)
  90. } else {
  91. log.Fatalln(err)
  92. }
  93. } else {
  94. log.Printf("Successfully created %s\n", bucketName)
  95. }
  96. //buckets, err := minioClient.ListBuckets(ctx)
  97. //for _, bucket := range buckets {
  98. // fmt.Println(bucket)
  99. //}
  100. // Upload the zip file
  101. objectName := "1111.xlsx"
  102. filePath := "/Users/xi/Desktop/1111.xlsx"
  103. contentType := "application/xlsx"
  104. // Upload the zip file with FPutObject
  105. info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
  106. if err != nil {
  107. log.Fatalln(err)
  108. }
  109. log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
  110. }
  111. // UploadImgToMinIo 图片上传
  112. func UploadImgToMinIo(fileName, filePath string) (string, error) {
  113. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  114. return "0", errors.New("MinIo信息未配置")
  115. }
  116. ctx := context.Background()
  117. endpoint := utils.MinIoEndpoint
  118. accessKeyID := utils.MinIoAccessKeyId
  119. secretAccessKey := utils.MinIoAccessKeySecret
  120. useSSL := false
  121. if utils.MinIoUseSSL == "true" {
  122. useSSL = true
  123. }
  124. minioClient, err := minio.New(endpoint, &minio.Options{
  125. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  126. Secure: useSSL,
  127. })
  128. if err != nil {
  129. log.Fatalln(err)
  130. return "1", err
  131. }
  132. bucketName := utils.MinIoBucketname
  133. // Check to see if we already own this bucket (which happens if you run this twice)
  134. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  135. if errBucketExists == nil && exists {
  136. log.Printf("We already own %s\n", bucketName)
  137. } else {
  138. log.Fatalln(err)
  139. return "2", err
  140. }
  141. path := utils.MinIoUploadDir + time.Now().Format("200601/20060102/")
  142. path += fileName
  143. // Upload the zip file with FPutObject
  144. //contentType := "application/xlsx"
  145. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  146. if err != nil {
  147. log.Fatalln(err)
  148. return "3", err
  149. }
  150. path = utils.MinIoImghost + path
  151. return path, err
  152. }
  153. // UploadAudioToMinIo 音频上传
  154. func UploadAudioToMinIo(fileName, filePath string) (string, error) {
  155. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  156. return "0", errors.New("MinIo信息未配置")
  157. }
  158. ctx := context.Background()
  159. endpoint := utils.MinIoEndpoint
  160. accessKeyID := utils.MinIoAccessKeyId
  161. secretAccessKey := utils.MinIoAccessKeySecret
  162. useSSL := false
  163. if utils.MinIoUseSSL == "true" {
  164. useSSL = true
  165. }
  166. minioClient, err := minio.New(endpoint, &minio.Options{
  167. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  168. Secure: useSSL,
  169. })
  170. if err != nil {
  171. log.Fatalln(err)
  172. return "1", err
  173. }
  174. bucketName := utils.MinIoBucketname
  175. // Check to see if we already own this bucket (which happens if you run this twice)
  176. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  177. if errBucketExists == nil && exists {
  178. log.Printf("We already own %s\n", bucketName)
  179. } else {
  180. log.Fatalln(err)
  181. return "2", err
  182. }
  183. path := utils.MinIoUpload_Audio_Dir + time.Now().Format("200601/20060102/")
  184. path += fileName
  185. // Upload the zip file with FPutObject
  186. //contentType := "application/xlsx"
  187. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  188. if err != nil {
  189. log.Fatalln(err)
  190. return "3", err
  191. }
  192. path = utils.MinIoImghost + path
  193. return path, err
  194. }
  195. // UploadVideoToMinIo 视频上传
  196. func UploadVideoToMinIo(filename, filePath, savePath string) error {
  197. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  198. return errors.New("MinIo信息未配置")
  199. }
  200. defer func() {
  201. os.Remove(filePath)
  202. }()
  203. ctx := context.Background()
  204. endpoint := utils.MinIoEndpoint
  205. accessKeyID := utils.MinIoAccessKeyId
  206. secretAccessKey := utils.MinIoAccessKeySecret
  207. useSSL := false
  208. if utils.MinIoUseSSL == "true" {
  209. useSSL = true
  210. }
  211. minioClient, err := minio.New(endpoint, &minio.Options{
  212. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  213. Secure: useSSL,
  214. })
  215. if err != nil {
  216. log.Fatalln(err)
  217. return err
  218. }
  219. bucketName := utils.MinIoBucketname
  220. // Check to see if we already own this bucket (which happens if you run this twice)
  221. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  222. if errBucketExists == nil && exists {
  223. log.Printf("We already own %s\n", bucketName)
  224. } else {
  225. log.Fatalln(err)
  226. return err
  227. }
  228. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  229. //path += filename
  230. _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
  231. if err != nil {
  232. log.Fatalln(err)
  233. return err
  234. }
  235. //path = utils.Imghost + path
  236. //return path,err
  237. return err
  238. }
  239. // UploadFileToMinIo 上传文件
  240. func UploadFileToMinIo(filename, filePath, savePath string) error {
  241. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  242. return errors.New("MinIo信息未配置")
  243. }
  244. defer func() {
  245. os.Remove(filePath)
  246. }()
  247. ctx := context.Background()
  248. endpoint := utils.MinIoEndpoint
  249. accessKeyID := utils.MinIoAccessKeyId
  250. secretAccessKey := utils.MinIoAccessKeySecret
  251. useSSL := false
  252. if utils.MinIoUseSSL == "true" {
  253. useSSL = true
  254. }
  255. minioClient, err := minio.New(endpoint, &minio.Options{
  256. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  257. Secure: useSSL,
  258. })
  259. if err != nil {
  260. log.Fatalln(err)
  261. return err
  262. }
  263. bucketName := utils.MinIoBucketname
  264. // Check to see if we already own this bucket (which happens if you run this twice)
  265. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  266. if errBucketExists == nil && exists {
  267. log.Printf("We already own %s\n", bucketName)
  268. } else {
  269. log.Fatalln(err)
  270. return err
  271. }
  272. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  273. //path += filename
  274. _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
  275. if err != nil {
  276. log.Fatalln(err)
  277. return err
  278. }
  279. //path = utils.Imghost + path
  280. //return path,err
  281. return err
  282. }
  283. // UploadMinIoToDir 上传至hzchart
  284. func UploadMinIoToDir(filename, filePath, uploadDir, fileDir string) (string, error) {
  285. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  286. return "0", errors.New("MinIo信息未配置")
  287. }
  288. ctx := context.Background()
  289. endpoint := utils.MinIoEndpoint
  290. accessKeyID := utils.MinIoAccessKeyId
  291. secretAccessKey := utils.MinIoAccessKeySecret
  292. useSSL := false
  293. if utils.MinIoUseSSL == "true" {
  294. useSSL = true
  295. }
  296. minioClient, err := minio.New(endpoint, &minio.Options{
  297. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  298. Secure: useSSL,
  299. })
  300. if err != nil {
  301. log.Fatalln(err)
  302. return "1", err
  303. }
  304. bucketName := utils.MinIoBucketname
  305. // Check to see if we already own this bucket (which happens if you run this twice)
  306. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  307. if errBucketExists == nil && exists {
  308. log.Printf("We already own %s\n", bucketName)
  309. } else {
  310. log.Fatalln(err)
  311. return "2", err
  312. }
  313. if uploadDir == "" {
  314. uploadDir = utils.MinIoUploadDir
  315. }
  316. if fileDir == "" {
  317. fileDir = time.Now().Format("200601/20060102/")
  318. }
  319. path := uploadDir + fileDir
  320. path += filename
  321. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  322. if err != nil {
  323. log.Fatalln(err)
  324. return "3", err
  325. }
  326. path = utils.MinIoImghost + path
  327. return path, err
  328. }
  329. func UploadImgToMinIoTest(fileName, filePath string) (string, error) {
  330. ctx := context.Background()
  331. endpoint := utils.Endpoint
  332. accessKeyID := utils.AccessKeyId
  333. secretAccessKey := utils.AccessKeySecret
  334. useSSL := false
  335. if utils.MinIoUseSSL == "true" {
  336. useSSL = true
  337. }
  338. minioClient, err := minio.New(endpoint, &minio.Options{
  339. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  340. Secure: useSSL,
  341. })
  342. if err != nil {
  343. log.Fatalln(err)
  344. return "1", err
  345. }
  346. bucketName := utils.Bucketname
  347. // Check to see if we already own this bucket (which happens if you run this twice)
  348. buckets, e := minioClient.ListBuckets(ctx)
  349. if e != nil {
  350. fmt.Println("ListBuckets: ", e.Error())
  351. return "", e
  352. }
  353. for k := range buckets {
  354. fmt.Println(k)
  355. }
  356. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  357. fmt.Println("exists: ", exists)
  358. fmt.Println("errBucketExists: ", errBucketExists)
  359. if errBucketExists == nil && exists {
  360. log.Printf("We already own %s\n", bucketName)
  361. } else {
  362. log.Fatalln(err)
  363. return "2", err
  364. }
  365. path := utils.UploadDir + time.Now().Format("200601/20060102/")
  366. path += fileName
  367. // Upload the zip file with FPutObject
  368. //contentType := "application/xlsx"
  369. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  370. if err != nil {
  371. log.Fatalln(err)
  372. return "3", err
  373. }
  374. path = utils.Imghost + path
  375. return path, err
  376. }