minio.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/minio/minio-go/v6"
  5. "io"
  6. "log"
  7. hurl "net/url"
  8. "smart-supplier-management-gateway/parser"
  9. "time"
  10. )
  11. func UploadToMinio(fileName string, r io.Reader, size int64, imgMine string) (objName string, err error) {
  12. endpoint := parser.Conf.Oss.Endpoint
  13. accessKeyID := parser.Conf.Oss.Id
  14. secretAccessKey := parser.Conf.Oss.Key
  15. useSSL := false
  16. // Initialize minio client object.
  17. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  18. if err != nil {
  19. return "", err
  20. }
  21. // Make a new bucket called mymusic.
  22. bucketName := parser.Conf.Oss.ProviderBucket
  23. // Upload the zip file
  24. objectName := fmt.Sprintf("%d", time.Now().Unix())+fileName
  25. contentType := imgMine
  26. // Upload the zip file with FPutObject
  27. _, err = minioClient.PutObject(bucketName, objectName, r, size, minio.PutObjectOptions{ContentType:contentType})
  28. if err != nil {
  29. return "", err
  30. }
  31. return parser.Conf.Oss.Protocol+"://"+endpoint+"/"+bucketName+"/"+objectName, nil
  32. }
  33. func GetFilePath(objName string) (string, error) {
  34. endpoint := parser.Conf.Oss.Endpoint
  35. accessKeyID := parser.Conf.Oss.Id
  36. secretAccessKey := parser.Conf.Oss.Key
  37. useSSL := false
  38. // Initialize minio client object.
  39. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  40. if err != nil {
  41. return "", err
  42. }
  43. // Make a new bucket called mymusic.
  44. bucketName := parser.Conf.Oss.ProviderBucket
  45. rr, er := minioClient.PresignedGetObject(bucketName, objName, 24*time.Hour, hurl.Values{})
  46. if er != nil {
  47. fmt.Printf("获取文件路径失败:%v\n", er)
  48. return "", er
  49. }
  50. return rr.String(), nil
  51. }
  52. func MiniTest() {
  53. endpoint := "47.108.135.38:9000"
  54. accessKeyID := "minioadmin"
  55. secretAccessKey := "hly@1353406"
  56. useSSL := false
  57. // Initialize minio client object.
  58. minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
  59. if err != nil {
  60. log.Fatalln(err)
  61. }
  62. // Make a new bucket called mymusic.
  63. bucketName := "testb"
  64. location := ""
  65. err = minioClient.MakeBucket(bucketName, location)
  66. if err != nil {
  67. // Check to see if we already own this bucket (which happens if you run this twice)
  68. exists, errBucketExists := minioClient.BucketExists(bucketName)
  69. if errBucketExists == nil && exists {
  70. log.Printf("We already own %s\n", bucketName)
  71. } else {
  72. log.Fatalln(err)
  73. }
  74. } else {
  75. log.Printf("Successfully created %s\n", bucketName)
  76. }
  77. // Upload the zip file
  78. objectName := "5.png"
  79. filePath := "D:\\5.png"
  80. contentType := ""
  81. // Upload the zip file with FPutObject
  82. n, err := minioClient.FPutObject(bucketName, objectName, filePath, minio.PutObjectOptions{ContentType:contentType})
  83. if err != nil {
  84. log.Fatalln(err)
  85. }
  86. log.Printf("Successfully uploaded %s of size %d\n", objectName, n)
  87. rr, er := minioClient.PresignedGetObject("testb", objectName, 24 * 365 * 100 *time.Hour, hurl.Values{})
  88. if er != nil {
  89. fmt.Printf("xxxx:%v\n", er)
  90. return
  91. }
  92. fmt.Printf("xxxx:%s\n", rr.String())
  93. }