169 lines
3.7 KiB
Go
169 lines
3.7 KiB
Go
// Package s3 is a package for s3
|
|
package s3
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"mime/multipart"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
)
|
|
|
|
// Client ...
|
|
var Client *minio.Client
|
|
|
|
// Init ...
|
|
func Init() error {
|
|
c := context.Background()
|
|
endpoint := os.Getenv("S3_ENDPOINT")
|
|
accessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
|
|
secretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
|
|
useSSL, errUseSSL := strconv.ParseBool(os.Getenv("S3_USE_SSL"))
|
|
if errUseSSL != nil {
|
|
return errUseSSL
|
|
}
|
|
|
|
println("S3 endpoint: ", endpoint)
|
|
client, err := minio.New(endpoint, &minio.Options{
|
|
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
|
Secure: useSSL,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
Client = client
|
|
|
|
backetName := os.Getenv("S3_BUCKET")
|
|
region := os.Getenv("S3_REGION")
|
|
|
|
println("S3 bucket: ", backetName)
|
|
if err := Client.MakeBucket(c, backetName, minio.MakeBucketOptions{Region: region}); err != nil {
|
|
println("Error can't create bucket ", err.Error())
|
|
// Check to see if we already own this bucket (which happens if you run this twice)
|
|
exists, err := Client.BucketExists(c, backetName)
|
|
println("Bucket exists: ", exists)
|
|
if err == nil && exists {
|
|
println("We already own %s\n", backetName)
|
|
} else {
|
|
println("Error can't create bucket ", err.Error())
|
|
return err
|
|
}
|
|
}
|
|
|
|
println("S3 init success")
|
|
return nil
|
|
}
|
|
|
|
// Upload ...
|
|
func Upload(file *multipart.FileHeader, fileName, path string) (string, error) {
|
|
fileOpen, errOpen := file.Open()
|
|
if errOpen != nil {
|
|
return "", errOpen
|
|
}
|
|
defer fileOpen.Close()
|
|
|
|
src := path + fileName
|
|
|
|
_, err := Client.PutObject(
|
|
context.Background(),
|
|
os.Getenv("S3_BUCKET"),
|
|
src,
|
|
fileOpen,
|
|
file.Size,
|
|
minio.PutObjectOptions{
|
|
ContentType: file.Header.Get("Content-Type"),
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return src, nil
|
|
}
|
|
|
|
// UploadWebpFile ...
|
|
func UploadWebpFile(file *os.File, src string) error {
|
|
|
|
fileInfo, err := file.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fileOpen, err := os.Open(file.Name())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
info, err := Client.PutObject(
|
|
context.Background(),
|
|
os.Getenv("S3_BUCKET"),
|
|
src,
|
|
fileOpen,
|
|
fileInfo.Size(),
|
|
minio.PutObjectOptions{
|
|
ContentType: "image/webp",
|
|
})
|
|
if err != nil {
|
|
fmt.Println("Error upload file: ", err.Error())
|
|
fmt.Println("info: ", fileInfo.Size())
|
|
return err
|
|
}
|
|
fmt.Println(info)
|
|
|
|
return nil
|
|
}
|
|
|
|
// CreatePathAndName name and path for file
|
|
func CreatePath(path string) (string, string) {
|
|
year, _, _ := time.Now().Date()
|
|
|
|
nameFold := strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
|
|
path = path + "/" + strconv.FormatInt(int64(year), 10) + "/" + nameFold + "/"
|
|
|
|
return nameFold, path
|
|
|
|
}
|
|
|
|
// UploadRandom is a function separate files by year and create random name
|
|
func UploadRandom(file *multipart.FileHeader, path string) (string, error) {
|
|
|
|
year, _, _ := time.Now().Date()
|
|
|
|
path = path + "/" + strconv.FormatInt(int64(year), 10) + "/"
|
|
|
|
fileName := strconv.FormatInt(time.Now().UnixNano(), 10) + "-" + file.Filename
|
|
|
|
return Upload(file, fileName, path)
|
|
}
|
|
|
|
// Remove ...
|
|
func Remove(src string) error {
|
|
c := context.Background()
|
|
return Client.RemoveObject(c, os.Getenv("S3_BUCKET"), src, minio.RemoveObjectOptions{})
|
|
}
|
|
|
|
// RemoveAll ...
|
|
func RemoveAll(srcs []string) error {
|
|
c := context.Background()
|
|
for _, src := range srcs {
|
|
if src == "" {
|
|
continue
|
|
}
|
|
if err := Client.RemoveObject(c, os.Getenv("S3_BUCKET"), src, minio.RemoveObjectOptions{}); err != nil {
|
|
return errors.New("Remove file error: " + err.Error())
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetObject ...
|
|
func GetObject(src string) (*minio.Object, error) {
|
|
c := context.Background()
|
|
return Client.GetObject(c, os.Getenv("S3_BUCKET"), src, minio.GetObjectOptions{})
|
|
}
|