minio.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package services
  2. import (
  3. "context"
  4. "errors"
  5. "eta/eta_mobile/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. return "1", err
  130. }
  131. bucketName := utils.MinIoBucketname
  132. // Check to see if we already own this bucket (which happens if you run this twice)
  133. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  134. if errBucketExists == nil && exists {
  135. log.Printf("We already own %s\n", bucketName)
  136. } else {
  137. log.Fatalln(err)
  138. return "2", err
  139. }
  140. path := utils.MinIoUploadDir + time.Now().Format("200601/20060102/")
  141. path += fileName
  142. // Upload the zip file with FPutObject
  143. //contentType := "application/xlsx"
  144. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  145. if err != nil {
  146. log.Fatalln(err)
  147. return "3", err
  148. }
  149. path = utils.MinIoImghost + path
  150. return path, err
  151. }
  152. // UploadAudioToMinIo 音频上传
  153. func UploadAudioToMinIo(fileName, filePath string) (string, error) {
  154. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  155. return "0", errors.New("MinIo信息未配置")
  156. }
  157. ctx := context.Background()
  158. endpoint := utils.MinIoEndpoint
  159. accessKeyID := utils.MinIoAccessKeyId
  160. secretAccessKey := utils.MinIoAccessKeySecret
  161. useSSL := false
  162. if utils.MinIoUseSSL == "true" {
  163. useSSL = true
  164. }
  165. minioClient, err := minio.New(endpoint, &minio.Options{
  166. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  167. Secure: useSSL,
  168. })
  169. if err != nil {
  170. log.Fatalln(err)
  171. return "1", err
  172. }
  173. bucketName := utils.MinIoBucketname
  174. // Check to see if we already own this bucket (which happens if you run this twice)
  175. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  176. if errBucketExists == nil && exists {
  177. log.Printf("We already own %s\n", bucketName)
  178. } else {
  179. log.Fatalln(err)
  180. return "2", err
  181. }
  182. path := utils.MinIoUpload_Audio_Dir + time.Now().Format("200601/20060102/")
  183. path += fileName
  184. // Upload the zip file with FPutObject
  185. //contentType := "application/xlsx"
  186. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  187. if err != nil {
  188. log.Fatalln(err)
  189. return "3", err
  190. }
  191. path = utils.MinIoImghost + path
  192. return path, err
  193. }
  194. // UploadVideoToMinIo 视频上传
  195. func UploadVideoToMinIo(filename, filePath, savePath string) error {
  196. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  197. return errors.New("MinIo信息未配置")
  198. }
  199. defer func() {
  200. os.Remove(filePath)
  201. }()
  202. ctx := context.Background()
  203. endpoint := utils.MinIoEndpoint
  204. accessKeyID := utils.MinIoAccessKeyId
  205. secretAccessKey := utils.MinIoAccessKeySecret
  206. useSSL := false
  207. if utils.MinIoUseSSL == "true" {
  208. useSSL = true
  209. }
  210. minioClient, err := minio.New(endpoint, &minio.Options{
  211. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  212. Secure: useSSL,
  213. })
  214. if err != nil {
  215. log.Fatalln(err)
  216. return err
  217. }
  218. bucketName := utils.MinIoBucketname
  219. // Check to see if we already own this bucket (which happens if you run this twice)
  220. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  221. if errBucketExists == nil && exists {
  222. log.Printf("We already own %s\n", bucketName)
  223. } else {
  224. log.Fatalln(err)
  225. return err
  226. }
  227. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  228. //path += filename
  229. _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
  230. if err != nil {
  231. log.Fatalln(err)
  232. return err
  233. }
  234. //path = utils.Imghost + path
  235. //return path,err
  236. return err
  237. }
  238. // UploadFileToMinIo 上传文件
  239. func UploadFileToMinIo(filename, filePath, savePath string) error {
  240. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  241. return errors.New("MinIo信息未配置")
  242. }
  243. defer func() {
  244. os.Remove(filePath)
  245. }()
  246. ctx := context.Background()
  247. endpoint := utils.MinIoEndpoint
  248. accessKeyID := utils.MinIoAccessKeyId
  249. secretAccessKey := utils.MinIoAccessKeySecret
  250. useSSL := false
  251. if utils.MinIoUseSSL == "true" {
  252. useSSL = true
  253. }
  254. minioClient, err := minio.New(endpoint, &minio.Options{
  255. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  256. Secure: useSSL,
  257. })
  258. if err != nil {
  259. log.Fatalln(err)
  260. return err
  261. }
  262. bucketName := utils.MinIoBucketname
  263. // Check to see if we already own this bucket (which happens if you run this twice)
  264. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  265. if errBucketExists == nil && exists {
  266. log.Printf("We already own %s\n", bucketName)
  267. } else {
  268. log.Fatalln(err)
  269. return err
  270. }
  271. //path := utils.Upload_Audio_Dir + time.Now().Format("200601/20060102/")
  272. //path += filename
  273. _, err = minioClient.FPutObject(ctx, bucketName, savePath, filePath, minio.PutObjectOptions{})
  274. if err != nil {
  275. log.Fatalln(err)
  276. return err
  277. }
  278. //path = utils.Imghost + path
  279. //return path,err
  280. return err
  281. }
  282. // UploadMinIoToDir 上传至hzchart
  283. func UploadMinIoToDir(filename, filePath, uploadDir, fileDir string) (string, error) {
  284. if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
  285. return "0", errors.New("MinIo信息未配置")
  286. }
  287. ctx := context.Background()
  288. endpoint := utils.MinIoEndpoint
  289. accessKeyID := utils.MinIoAccessKeyId
  290. secretAccessKey := utils.MinIoAccessKeySecret
  291. useSSL := false
  292. if utils.MinIoUseSSL == "true" {
  293. useSSL = true
  294. }
  295. minioClient, err := minio.New(endpoint, &minio.Options{
  296. Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
  297. Secure: useSSL,
  298. })
  299. if err != nil {
  300. log.Fatalln(err)
  301. return "1", err
  302. }
  303. bucketName := utils.MinIoBucketname
  304. // Check to see if we already own this bucket (which happens if you run this twice)
  305. exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
  306. if errBucketExists == nil && exists {
  307. log.Printf("We already own %s\n", bucketName)
  308. } else {
  309. log.Fatalln(err)
  310. return "2", err
  311. }
  312. if uploadDir == "" {
  313. uploadDir = utils.MinIoUploadDir
  314. }
  315. if fileDir == "" {
  316. fileDir = time.Now().Format("200601/20060102/")
  317. }
  318. path := uploadDir + fileDir
  319. path += filename
  320. _, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
  321. if err != nil {
  322. log.Fatalln(err)
  323. return "3", err
  324. }
  325. path = utils.MinIoImghost + path
  326. return path, err
  327. }