minio.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package services
  2. import (
  3. "context"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/minio/minio-go/v7"
  7. "github.com/minio/minio-go/v7/pkg/credentials"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "strings"
  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. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  112. body := ioutil.NopCloser(strings.NewReader(postData))
  113. client := &http.Client{}
  114. req, err := http.NewRequest("POST", url, body)
  115. if err != nil {
  116. return nil, err
  117. }
  118. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  119. if len(params) > 0 && params[0] != "" {
  120. contentType = params[0]
  121. }
  122. req.Header.Set("Content-Type", contentType)
  123. resp, err := client.Do(req)
  124. defer resp.Body.Close()
  125. b, err := ioutil.ReadAll(resp.Body)
  126. fmt.Println("HttpPost:" + string(b))
  127. return b, err
  128. }