其他分享
首页 > 其他分享> > minio API demo

minio API demo

作者:互联网

package main

import (
"context"
"fmt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"log"
)

func main() {
endpoint := "127.0.0.1:9000"
accessKeyID := "minioadmin"
secretAccessKey := "minioadmin"
useSSL := false
ctx := context.Background()
// Initialize minio client object.
minioClient, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}

//log.Printf("%#v\n", minioClient) // minioClient is now setup
//// Create a bucket at region 'us-east-1' with object locking enabled.
//err = minioClient.MakeBucket(ctx, "gwx",minio.MakeBucketOptions{})
//if err != nil {
// fmt.Println("******************",err)
// return
//}
//fmt.Println("Successfully created mybucket.")

//buckets, err := minioClient.ListBuckets(ctx)
//if err != nil {
// fmt.Println(err)
// return
//}
//for _, bucket := range buckets {
// fmt.Println(bucket)
//}


ctx, cancel := context.WithCancel(context.Background())

defer cancel()

objectCh := minioClient.ListObjects(ctx, "test", minio.ListObjectsOptions{
Prefix: "",
Recursive: true,
})
var totalSize int64
for object := range objectCh {
if object.Err != nil {
fmt.Println(object.Err)
return
}
totalSize += object.Size
fmt.Println(object,"******",object.Size,totalSize,object.LastModified)

}

//err = minioClient.RemoveBucket(context.Background(), "gwx")
//if err != nil {
// fmt.Println(err)
// return
//}



objectsCh := make(chan minio.ObjectInfo)
// Send object names that are needed to be removed to objectsCh
go func() {
defer close(objectsCh)
// List all objects from a bucket-name with a matching prefix.
for object := range minioClient.ListObjects(ctx, "test", minio.ListObjectsOptions{
Prefix: "对象数据集/模拟-考试题",
Recursive: true,
}) {
if object.Err != nil {
log.Fatalln(object.Err)
}
fmt.Println(object)
objectsCh <- object
}
}()

opts := minio.RemoveObjectsOptions{
GovernanceBypass: true,
}

for rErr := range minioClient.RemoveObjects(context.Background(), "test", objectsCh, opts) {
fmt.Println("Error detected during deletion: ", rErr)
}



//file, err := os.Open("D:\\huawei-oj\\test\\work\\main.go.bk")
//if err != nil {
// fmt.Println(err)
// return
//}
//defer file.Close()
//
//fileStat, err := file.Stat()
//if err != nil {
// fmt.Println(err)
// return
//}
//
//n, err := minioClient.PutObject(ctx, "test", "对象数据集/test.txt", file, fileStat.Size(), minio.PutObjectOptions{ContentType:"application/octet-stream"})
//if err != nil {
// fmt.Println(err)
// return
//}
//fmt.Println("Successfully uploaded bytes: ", n)




//objInfo, err := minioClient.StatObject(ctx,"test", "test.txt", minio.StatObjectOptions{})
//if err != nil {
// fmt.Println(err)
// return
//}
//fmt.Println(objInfo)


//object, err := minioClient.GetObject(ctx,"test", "test.txt", minio.GetObjectOptions{})
//if err != nil {
// fmt.Println(err)
// return
//}
//buf := make([]byte,1024,1024)
//object.Read(buf)
//
//fmt.Println(object.Read(buf))

//func get(w http.ResponseWriter, r *http.Request) {
// // 收到 接口服务器的 请求
// // 提取 要获取的文件名
// name := strings.Split(r.URL.EscapedPath(), "/")[2]
// f, e := os.Open(objectDir + name)
// if e != nil {
// log.Println(e)
// w.WriteHeader(http.StatusNotFound)
// return
// }
// defer f.Close()
// // 真正读取,并发送
// io.Copy(w, f)
//}

//localFile, err := os.Create("/tmp/test.txt")
//if err != nil {
// fmt.Println(err)
// return
//}
//if _, err = io.Copy(localFile, object); err != nil {
// fmt.Println(err)
// return
//}


//opts := minio.RemoveObjectOptions{
//}
//
//err = minioClient.RemoveObject(ctx,"test","对象数据集",opts)
//if err != nil {
// fmt.Println("********", err)
// return
//}
//
//fmt.Println("********", err)


}

标签:minio,err,nil,demo,fmt,object,API,Println
来源: https://www.cnblogs.com/gongxianjin/p/16510637.html