osi

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

osi

A universal object store interface that has implemented s3, oss, minio, cos, obs, and local storage media 中文版

Current supported

  • s3
  • oss
  • minio
  • cos
  • obs
  • local (local file system)

Install

go get github.com/burybell/osi

Usage

Initialize Instance

First Method

use sugar package

store := sugar.MustNewObjectStore(sugar.UseS3(s3.Config{
    Region: "example",
    KeyID:  "example",
    Secret: "example",
}))
Second Method

use implementation package

store,err := minio.NewObjectStore(minio.Config{
    Region: "example",
    KeyID:  "example",
    Secret: "example",
    Endpoint: "localhost:9000",
    UseSSL: false
})

object operation

package main

import (
	"github.com/burybell/osi/s3"
	"github.com/burybell/osi/sugar"
)

func main() {
	store := sugar.MustNewObjectStore(sugar.UseS3(s3.Config{
		Region: "example",
		KeyID:  "example",
		Secret: "example",
	}))
	bucket := store.Bucket("example")

	//put object
	err := bucket.PutObject("path/to/file", io.NopCloser(strings.NewReader("some data")))
	if err != nil {
		panic(err)
	}

	// put object with acl
	err = bucket.PutObjectWithACL("path/to/file", io.NopCloser(strings.NewReader("some data")), store.ACLEnum().PublicRead())
	if err != nil {
		panic(err)
	}

	// get object
	obj, err := bucket.GetObject("path/to/file")
	if err != nil {
		panic(err)
	}

	// get object size
	sz, err := bucket.GetObjectSize("path/to/file")
	if err != nil {
		panic(err)
	}
	fmt.Println(sz.Size())

	// check object exist
	exist, err := bucket.HeadObject("path/to/file")
	if err != nil {
		panic(err)
	}
	fmt.Println(exist)

	f, err := os.Open("path/to/file")
	if err != nil {
		panic(err)
	}
	_, _ = io.Copy(f, obj)
	_ = f.Close()

	// list object
	objects, err := bucket.ListObject("/path")
	if err != nil {
		panic(err)
	}

	for i := range objects {
		// delete object
		_ = bucket.DeleteObject(objects[i].ObjectPath())
	}

	url, err := bucket.SignURL("path/to/file", http.MethodGet, time.Second*100)
	if err != nil {
		panic(err)
	}
	fmt.Println(url)
}

use local file system for testing or other

package main

import (
	"github.com/burybell/osi/local"
	"github.com/burybell/osi/sugar"
	"log"
	"net/http"
	"time"
)

func main() {

	store := sugar.MustNewObjectStore(sugar.UseLocal(local.Config{
		BasePath:   "/tmp/osi",
		HttpAddr:   "http://localhost:8080",
		HttpSecret: "example",
	}))

	//  handle sign url, you can get a signed url for upload or download
	http.HandleFunc("/sign_url", func(w http.ResponseWriter, r *http.Request) {
		url, err := store.Bucket("example").SignURL(r.URL.Query().Get("path"), r.Method, time.Second*1000)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.Write([]byte(url))
	})
	log.Fatalln(http.ListenAndServe(":8080", nil))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ObjectNotFound = errors.New("ObjectNotFound")
)

Functions

This section is empty.

Types

type ACL

type ACL = string

type ACLEnum

type ACLEnum interface {
	Private() ACL
	PublicRead() ACL
	PublicReadWrite() ACL
	Default() ACL
}

type Bucket

type Bucket interface {
	BucketObject
	BucketObjects
	ObjectSigner
}

type BucketObject

type BucketObject interface {
	GetObject(ctx context.Context, path string) (Object, error)
	PutObject(ctx context.Context, path string, reader io.Reader) error
	PutObjectWithACL(ctx context.Context, path string, reader io.Reader, acl ACL) error
	HeadObject(ctx context.Context, path string) (bool, error)
	DeleteObject(ctx context.Context, path string) error
	GetObjectSize(ctx context.Context, path string) (Size, error)
}

type BucketObjects

type BucketObjects interface {
	ListObjects(ctx context.Context, prefix string) ([]ObjectMeta, error)
	DeleteObjects(ctx context.Context, paths []string) error
}

type Object

type Object interface {
	ObjectMeta
	io.ReadCloser
	ObjectACL() ACL
}

func NewObject

func NewObject(bucket string, path string, acl ACL, reader io.ReadCloser) Object

type ObjectMeta

type ObjectMeta interface {
	Bucket() string
	ObjectPath() string
	Extension() string
}

func NewObjectMeta

func NewObjectMeta(bucket string, path string) ObjectMeta

type ObjectSigner

type ObjectSigner interface {
	SignURL(ctx context.Context, path string, method string, expiredInDur time.Duration) (string, error)
}

type ObjectStore

type ObjectStore interface {
	Name() string
	Bucket(name string) Bucket
	ACLEnum() ACLEnum
}

type Size

type Size interface {
	Size() int64
}

func NewSize

func NewSize(sz int64) Size

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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