Skip to content

Latest commit

 

History

History

oss

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Media Library OSS

Use OSS as backend to store medias

Usage

import (
	"github.com/qor/media/oss"
	"github.com/qor/oss/filesystem"
	"github.com/qor/oss/s3"
	awss3 "github.com/aws/aws-sdk-go/service/s3"
)

type Product struct {
	gorm.Model
	Image oss.OSS
}

func init() {
  // OSS's default storage is directory `public`, change it to S3
	oss.Storage = s3.New(&s3.Config{AccessID: "access_id", AccessKey: "access_key", Region: "region", Bucket: "bucket", Endpoint: "cdn.getqor.com", ACL: awss3.BucketCannedACLPublicRead})

  // or change directory to `download`
	oss.Storage = filesystem.New("download")
}

Advanced Usage

// change URL template
oss.URLTemplate = "/system/{{class}}/{{primary_key}}/{{column}}/{{filename_with_hash}}"

// change default URL handler
oss.DefaultURLTemplateHandler = func(option *media_library.Option) (url string) {
  // ...
}

// change default save handler
oss.DefaultStoreHandler = func(path string, option *media_library.Option, reader io.Reader) error {
  // ...
}

// change default retrieve handler
oss.DefaultRetrieveHandler = func(path string) (*os.File, error) {
	// ...
}

// By overwritting default store, retrieve handler, you could do some advanced tasks, like use private mode when store sensitive data to S3, public read mode for other files

License

Released under the MIT License.