storage

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

集成第三方对象存储 oss、cos、七牛、本地

创建配置文件 yaml

Store: local
MaxFileSize: 1073741824

OSS:
  BucketName: ""
  Endpoint: ""
  AccessId: ""
  AccessSecret: ""

Local:
  BaseDir: "upload/images"
  BaseUrl: "http://localhost:8888/static"

COS:
  AppId: ""
  BucketName: ""
  Region: ""
  SecretId: ""
  SecretKey: ""

QN:
  AccessKey: ""
  SecretKey: ""
  SiteUrl: ""
  BucketName: ""

使用

// 支持 local、cos、oss 、qn(七牛)存储
// parse to config
type conf struct {
    Store       string
    MaxFileSize int64
    OSS         storage.OSSConfig
    Local       storage.LocalConfig
    COS         storage.COSConfig
    QN          storage.QNConfig
}
storage.Parse(conf.Store,conf.MaxFileSize,conf.OSS)
// conf.Store 与conf.OSS 对应

// form表单上传文件 r : *http.Request
storage.Put(l.r, "myFile", "test", "test1.png") // 指定文件名
storage.Put(l.r, "myFile", "test") // 随机生成文件名,文件扩展一致

// 本地文件路径上传
storage.PutFromFile(filePath, "test", "test2.png") // 指定文件名
storage.PutFromFile(filePath, "test") // 随机生成文件名,文件扩展一致

// 删除文件
storage.Delete("test/test1.png") // 相对路径

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(path string) error

func Put

func Put(r *http.Request, fileField, toFilePath string, fileName ...string) (string, error)

func PutFromFile

func PutFromFile(filePath, toFilePath string, fileName ...string) (string, error)

Types

type COSConfig

type COSConfig struct {
	BucketName string
	AppId      string
	Region     string
	SecretId   string
	SecretKey  string
}

type Client

type Client interface {
	Put(r *http.Request, fileField, toFilePath string, fileName ...string) (string, error)
	PutFromFile(filePath, toFilePath string, fileName ...string) (string, error)
	Delete(path string) error
}

func Parse

func Parse(store string, maxFileSize int64, conf interface{}) Client

type LocalConfig

type LocalConfig struct {
	BaseDir string
	BaseUrl string
}

type LocalInstance

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

func (*LocalInstance) Delete

func (c *LocalInstance) Delete(path string) error

func (*LocalInstance) Put

func (c *LocalInstance) Put(r *http.Request, fileField, toFilePath string, fileName ...string) (string, error)

func (*LocalInstance) PutFromFile

func (c *LocalInstance) PutFromFile(filePath, toFilePath string, fileName ...string) (string, error)

type OSSConfig

type OSSConfig struct {
	BucketName   string
	Endpoint     string
	AccessId     string
	AccessSecret string
}

type QNConfig

type QNConfig struct {
	AccessKey  string
	SecretKey  string
	SiteUrl    string
	BucketName string
}

Jump to

Keyboard shortcuts

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