commit e5589cc8b21e595cd6fe0c946f6ce5260ded04c7 Author: dustoair <107600816+dustoair@users.noreply.github.com> Date: Mon Jul 25 21:11:33 2022 +0800 open src diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e6b069 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +go.sum \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..6db0b70 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ + +## build +cd /root +rm -rf goMinioUpload +git clone https://git.sre.ink/go/goMinioUpload.git +cd goMinioUpload +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o mupload main.go + + +## deploy +cd /usr/bin/ +rm -rf /usr/bin/mupload +rz +chmod +x /usr/bin/mupload + +## usage +chmod +x /usr/bin/mupload +mupload test.txt + +## build for arm64 +CGO_ENABLED=0 GOARM=7 GOOS=linux GOARCH=arm64 GO111MODULE=on GOPROXY="https://goproxy.cn,direct" go build -v -a -o mupload main.go + \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..be435c4 --- /dev/null +++ b/go.mod @@ -0,0 +1,19 @@ +module goMinioUpload + +go 1.18 + +require ( + github.com/json-iterator/go v1.1.9 // indirect + github.com/klauspost/cpuid v1.2.3 // indirect + github.com/minio/md5-simd v1.1.0 // indirect + github.com/minio/minio-go/v6 v6.0.57 // indirect + github.com/minio/sha256-simd v0.1.1 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect + golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f // indirect + golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect + golang.org/x/sys v0.0.0-20190422165155-953cdadca894 // indirect + golang.org/x/text v0.3.0 // indirect + gopkg.in/ini.v1 v1.42.0 // indirect +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..a98c3d4 --- /dev/null +++ b/main.go @@ -0,0 +1,104 @@ +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) + +}