oss

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

oss

A universal OSS interface that has implemented s3, aliyun, mini, tent (cos), and local storage media

Current supported

  • s3
  • aliyun
  • mini
  • tencent (cos)
  • local (local file system)
  • qiniu
  • huawei

Install

go get github.com/burybell/oss

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/oss/s3"
	"github.com/burybell/oss/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/oss/local"
	"github.com/burybell/oss/sugar"
	"log"
	"net/http"
	"time"
)

func main() {

	store := sugar.MustNewObjectStore(sugar.UseLocal(local.Config{
		BasePath:   "/tmp/oss",
		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 {
	GetObject(path string) (Object, error)
	PutObject(path string, reader io.Reader) error
	PutObjectWithACL(path string, reader io.Reader, acl ACL) error
	HeadObject(path string) (bool, error)
	DeleteObject(path string) error
	ListObject(prefix string) ([]ObjectMeta, error)
	GetObjectSize(path string) (Size, error)
	SignURL(path string, method string, expiredInDur time.Duration) (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 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