minio.go 9.6 KB

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