gominio

package
v1.2.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

go.minio

基于 github.com/minio/minio-go/v7 封装

package main

import (
	"context"
	"flag"
	"fmt"
	gominio "github.com/gif-gif/go.io/go-minio"
	"github.com/minio/minio-go/v7"
	"net/url"
	"os"
	"strings"
	"time"
)

var (
	AccessKeyId     = flag.String("access_key_id", "fPztco0yxWC1qrxz6iDN", "")
	AccessKeySecret = flag.String("access_key_secret", "kWS6q1PIxjcEnLSkoBWrWqPnsHFHm1Q8cvG1CUPm", "")
	Endpoint        = flag.String("endpoint", "minio.gif00.com", "")
	Bucket          = flag.String("bucket", "test", "")
	Domain          = flag.String("domain", "", "")
)

func createBucketTest() {
	conf := gominio.Config{
		AccessKeyId:     *AccessKeyId,
		AccessKeySecret: *AccessKeySecret,
		Endpoint:        *Endpoint,
		Bucket:          *Bucket,
		Domain:          *Domain,
		UseSSL:          false,
	}

	oss := gominio.New(conf)
	bucketName := "testbucket"
	location := "us-east-1"

	err := oss.CreateBucket(context.Background(), bucketName, location)
	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println("Created bucket successfully")
	}

}

func uploadTest() {
	args := os.Args
	if l := len(args); l < 2 {
		fmt.Println("请选择上传文件!")
		return
	}

	conf := gominio.Config{
		AccessKeyId:     *AccessKeyId,
		AccessKeySecret: *AccessKeySecret,
		Endpoint:        *Endpoint,
		Bucket:          *Bucket,
		Domain:          *Domain,
		UseSSL:          false,
	}

	oss := gominio.New(conf)

	for n, i := range args {
		if n == 0 {
			continue
		}

		var filename string
		index := strings.LastIndex(i, "/")
		if index == -1 {
			filename = i
		} else {
			filename = i[index+1:]
		}

		info, err := oss.FPutObject(context.Background(), strings.ToLower(filename), i, &minio.PutObjectOptions{
			Expires: time.Now().AddDate(0, 0, 1),
		})
		if err != nil {
			fmt.Println(err.Error())
			continue
		}
		fmt.Println(info)
	}
}

func getTest() {

	conf := gominio.Config{
		AccessKeyId:     *AccessKeyId,
		AccessKeySecret: *AccessKeySecret,
		Endpoint:        *Endpoint,
		Bucket:          *Bucket,
		Domain:          *Domain,
		UseSSL:          false,
	}

	oss := gominio.New(conf)

	err := oss.FGetObject(context.Background(), "/test/2024/05/422744271b108960a4818cc91a1822d9.log", "/Users/Jerry/Desktop/bak202405/422744271b108960a4818cc91a1822d9.log", nil)
	if err != nil {
		fmt.Println(err.Error())
	} else {
		fmt.Println("get object succeeded")
	}

	for object := range oss.ListObjects("test", nil) {
		if object.Err != nil {
			fmt.Println(object.Err)
			return
		}
		fmt.Println("found:", object)
	}

	// Set request parameters for content-disposition.
	reqParams := make(url.Values)
	reqParams.Set("response-content-disposition", "attachment; filename=\"your-filename.txt\"")

	// Generates a presigned url which expires in a day.
	presignedURL, err := oss.PresignedGetObject("test", "test.apk", time.Second*24*60*60, reqParams)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Successfully generated presigned URL", presignedURL)
}

func main() {
	//uploadTest()
	//createBucketTest()
	getTest()
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	AccessKeyId     string `json:",optional" yaml:"AccessKeyId"`
	AccessKeySecret string `json:",optional" yaml:"AccessKeySecret"`
	Endpoint        string `json:",optional" yaml:"Endpoint"`
	Bucket          string `json:",optional" yaml:"Bucket"`
	Dir             string `json:",optional" yaml:"Dir"`
	Domain          string `json:",optional" yaml:"Domain,optional"`
	UseSSL          bool   `json:",optional" yaml:"UseSSL"`
}

type GoMinio

type GoMinio struct {
	// contains filtered or unexported fields
}

func (*GoMinio) BucketExists

func (o *GoMinio) BucketExists(bucketName string) (bool, error)

func (*GoMinio) CreateBucket

func (o *GoMinio) CreateBucket(ctx context.Context, bucketName string, location string) error

func (*GoMinio) FGetObject

func (o *GoMinio) FGetObject(ctx context.Context, objectName, saveFilePath string, options *minio.GetObjectOptions) error

func (*GoMinio) FPutObject

func (o *GoMinio) FPutObject(ctx context.Context, objectName, filePath string, options *minio.PutObjectOptions) (*minio.UploadInfo, error)

func (*GoMinio) ListBuckets

func (o *GoMinio) ListBuckets() ([]minio.BucketInfo, error)

func (*GoMinio) ListObjects

func (o *GoMinio) ListObjects(bucketName string, opts *minio.ListObjectsOptions) <-chan minio.ObjectInfo

func (*GoMinio) PresignedGetObject

func (o *GoMinio) PresignedGetObject(bucketName, objectName string, expiry time.Duration, reqParams url.Values) (u *url.URL, err error)

type Oss

type Oss interface {
	Init(conf Config) *GoMinio
	Client() *minio.Client
	FPutObject(ctx context.Context, objectName string, sourceFilePath string, options *minio.PutObjectOptions) (*minio.UploadInfo, error)
	FGetObject(ctx context.Context, objectName, saveFilePath string, options *minio.GetObjectOptions) error
	ListObjects(bucketName string, opts *minio.ListObjectsOptions) <-chan minio.ObjectInfo
	ListBuckets() ([]minio.BucketInfo, error)
	CreateBucket(ctx context.Context, bucketName string, location string) error
	// 生成原始下载地址
	PresignedGetObject(bucketName, objectName string, expiry time.Duration, reqParams url.Values) (u *url.URL, err error)
}

func New

func New(conf Config) Oss

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL