123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package services
- import (
- "context"
- "eta/eta_api/utils"
- "fmt"
- "github.com/minio/minio-go/v7"
- "github.com/minio/minio-go/v7/pkg/credentials"
- "io/ioutil"
- "log"
- "net/http"
- "strings"
- )
- func GetMinIOSTSToken() (item *Token, err error) {
- // MinIO服务的访问信息
- item = new(Token)
- //useSSL := false
- //if utils.MinIoUseSSL == "true" {
- // useSSL = true
- //}
- // 创建MinIO客户端
- //minioClient, err := minio.New(utils.MinIoEndpoint, &minio.Options{
- // Creds: credentials.NewStaticV4(utils.MinIoAccessKeyId, utils.MinIoAccessKeySecret, ""),
- // Secure: useSSL,
- //})
- //if err != nil {
- // return nil, err
- //}
- // 设置STS凭证请求参数
- //policy := `{
- // "Version": "2012-10-17",
- // "Statement": [
- // {
- // "Sid": "",
- // "Effect": "Allow",
- // "Principal": {"AWS": "arn:aws:iam::1234567890:root"},
- // "Action": "s3:GetObject",
- // "Resource": "arn:aws:s3:::<YourBucketName>/*"
- // }
- // ]
- //}`
- //expiry := time.Hour * 24 // STS凭证的过期时间
- //获取STS凭证
- //stsCredentials, err := minioClient.PresignedPutObject(context.Background(), "etastatic", "myobject", expiry)
- //if err != nil {
- // return
- //}
- item.AccessKeyId = utils.MinIoAccessKeyId
- item.SecretKeyId = utils.MinIoAccessKeySecret
- item.Endpoint = utils.MinIoEndpoint
- item.ImgHost = utils.MinIoImghost
- item.Bucketname = utils.MinIoBucketname
- item.UseSSL = utils.MinIoUseSSL
- item.RegionId = utils.MinIoRegion
- item.Port = utils.MinIoPort
- return
- }
- type Token struct {
- AccessKeyId string
- SecretKeyId string
- RegionId string
- Bucketname string
- Endpoint string
- ImgHost string
- UseSSL string
- Port string
- }
- func UploadMinIo() {
- ctx := context.Background()
- endpoint := "8.136.199.33:9000/"
- accessKeyID := "LfQ8uiJiLP7vLxjRrmNW"
- secretAccessKey := "IszGVHsNicJMQxHC46cYFtbrOiapo0ynwOIJ6c2R"
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, &minio.Options{
- Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
- Secure: useSSL,
- })
- if err != nil {
- log.Fatalln(err)
- }
- // Make a new bucket called mymusic.
- bucketName := "etastatic"
- location := "/"
- err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
- if err != nil {
- // Check to see if we already own this bucket (which happens if you run this twice)
- exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
- if errBucketExists == nil && exists {
- log.Printf("We already own %s\n", bucketName)
- } else {
- log.Fatalln(err)
- }
- } else {
- log.Printf("Successfully created %s\n", bucketName)
- }
- buckets, err := minioClient.ListBuckets(ctx)
- for _, bucket := range buckets {
- fmt.Println(bucket)
- }
- // Upload the zip file
- objectName := "1111.xlsx"
- filePath := "/Users/xi/Desktop/1111.xlsx"
- contentType := "application/xlsx"
- // Upload the zip file with FPutObject
- info, err := minioClient.FPutObject(ctx, bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
- if err != nil {
- log.Fatalln(err)
- }
- log.Printf("Successfully uploaded %s of size %d\n", objectName, info.Size)
- }
- func HttpPost(url, postData string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- resp, err := client.Do(req)
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- fmt.Println("HttpPost:" + string(b))
- return b, err
- }
|