You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"fmt"
"log"
"os"
"path"
"time"
)
func main() {
if len(os.Args) > 0 {
c := NewClient()
//c.checkBucket()
for index, value := range os.Args {
if index > 0 {
fmt.Println("当前上传", value)
c.uploadFile(value)
}
}
} else {
fmt.Println("请输入文件路径")
}
}
type client struct {
client *minio.Client
bucketName string
targetFilePath string
useSSL bool
location string
}
func NewClient() *client {
var endpoint = "xxxxxxxxxxxxxx:4455" //minio地址
var accessKeyID = "xxxxxxxxxxxxxxxxxxxx" //账号
var secretAccessKey = "xxxxxxxxxxxxxxxxxxxxxxx" //密码
var useSSL = true //使用http或https
target, err := minio.New(endpoint, accessKeyID, secretAccessKey, useSSL)
if err != nil {
log.Fatalln(err)
return nil
}
return &client{
client: target,
bucketName: "n1", //目标bucket
location: "cn-north-1", //中国
}
}
// checkBucket 检测目标bucket是否存在不存在就创建一个
func (c *client) checkBucket() {
isExists, err := c.client.BucketExists(c.bucketName)
if err != nil {
log.Println("check bucket exist error ")
return
}
if !isExists {
err2 := c.client.MakeBucket(c.bucketName, c.location)
if err2 != nil {
log.Println("MakeBucket error ")
fmt.Println(err2)
return
}
log.Printf("Successfully created %s\n", c.bucketName)
}
}
func (c *client) uploadFile(filePath string) {
_, errStat := os.Stat(filePath)
if os.IsNotExist(errStat) {
fmt.Println("文件不存在: ", filePath)
return
}
// 指定minio上的相对路径
filenameall := path.Base(filePath)
//datetime := time.Now().Format("2006-01-02/15-04-05")
datetime := time.Now().Format("2006-01-02")
objectName := datetime + "/" + filenameall
// 指定上传文件类型
//filenameall := path.Base(filePath)
//filesuffix := path.Ext(filePath)
//fileprefix := filenameall[0:len(filenameall) - len(filesuffix)]
//fmt.Println(filenameall)
//fmt.Println(filesuffix)
//fmt.Println(fileprefix)
//contentType := "application/zip"
// 调用 FPutObject 接口上传文件。
//n, err := c.client.FPutObject(c.bucketName, objectName, filePath, minio.PutObjectOptions{ContentType: contentType})
n, err := c.client.FPutObject(c.bucketName, objectName, filePath, minio.PutObjectOptions{})
if err != nil {
log.Fatalln("上传文件失败", err)
}
log.Printf("n: %s", n)
log.Printf("上传文件 %s 成功\n", objectName)
log.Printf("下载地址https://xxxxxxxxxxxxxxxxxxxxx:4455/gitea/" + objectName)
}