123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package utils
- import (
- "fmt"
- "github.com/minio/minio-go/v6"
- "io"
- "log"
- hurl "net/url"
- "smart-site-management-gateway/parser"
- "time"
- )
- func UploadToMinio(fileName string, r io.Reader, size int64, imgMine string) (objName string, err error) {
- endpoint := parser.Conf.Oss.Endpoint
- accessKeyID := parser.Conf.Oss.Id
- secretAccessKey := parser.Conf.Oss.Key
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- return "", err
- }
- // Make a new bucket called mymusic.
- bucketName := parser.Conf.Oss.ProjectBucket
- // Upload the zip file
- objectName := fmt.Sprintf("%d", time.Now().Unix())+fileName
- contentType := imgMine
- // Upload the zip file with FPutObject
- _, err = minioClient.PutObject(bucketName, objectName, r, size, minio.PutObjectOptions{ContentType:contentType})
- if err != nil {
- return "", err
- }
- return parser.Conf.Oss.Protocol +"://"+endpoint+"/"+bucketName+"/"+objectName, nil
- }
- func GetFilePath(objName string) (string, error) {
- endpoint := parser.Conf.Oss.Endpoint
- accessKeyID := parser.Conf.Oss.Id
- secretAccessKey := parser.Conf.Oss.Key
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- return "", err
- }
- // Make a new bucket called mymusic.
- bucketName := parser.Conf.Oss.ProjectBucket
- rr, er := minioClient.PresignedGetObject(bucketName, objName, 24*time.Hour, hurl.Values{})
- if er != nil {
- fmt.Printf("获取文件路径失败:%v\n", er)
- return "", er
- }
- return rr.String(), nil
- }
- func MiniTest() {
- endpoint := "47.108.135.38:9000"
- accessKeyID := "minioadmin"
- secretAccessKey := "hly@1353406"
- useSSL := false
- // Initialize minio client object.
- minioClient, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
- if err != nil {
- log.Fatalln(err)
- }
- // Make a new bucket called mymusic.
- bucketName := "testb"
- location := ""
- err = minioClient.MakeBucket(bucketName, location)
- if err != nil {
- // Check to see if we already own this bucket (which happens if you run this twice)
- exists, errBucketExists := minioClient.BucketExists(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)
- }
- // Upload the zip file
- objectName := "5.png"
- filePath := "D:\\5.png"
- contentType := ""
- // Upload the zip file with FPutObject
- n, err := minioClient.FPutObject(bucketName, objectName, filePath, minio.PutObjectOptions{ContentType:contentType})
- if err != nil {
- log.Fatalln(err)
- }
- log.Printf("Successfully uploaded %s of size %d\n", objectName, n)
- rr, er := minioClient.PresignedGetObject("testb", objectName, 24 * 365 * 100 *time.Hour, hurl.Values{})
- if er != nil {
- fmt.Printf("xxxx:%v\n", er)
- return
- }
- fmt.Printf("xxxx:%s\n", rr.String())
- }
|