Categorygithub.com/bastengao/go-storage
modulepackage
0.5.0
Repository: https://github.com/bastengao/go-storage.git
Documentation: pkg.go.dev

# README

storage

Go Reference

Inspired by ActiveStorage from Rails.

Service

  • Disk
  • AWS S3
  • Google Cloud Storage
  • MicroSoft Azure Storage

TODO

  • Public URL
  • Private URL (Signed URL)
  • Direct upload URL

Documentation

Usage

Use Service to manipulate files.

import (
  "bytes"
  "context"
  "log"

  "github.com/aws/aws-sdk-go-v2/config"
  "github.com/bastengao/go-storage"
)

cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
  log.Fatal(err)
}

service, err := storage.NewS3(cfg, "bucket", "https://bucket.us-east-1.s3.amazonaws.com")
if err != nil {
  log.Fatal(err)
}

err = service.Upload(context.TODO(), "test/abc.txt", bytes.NewReader([]byte("hello world")))
if err != nil {
  log.Fatal(err)
}

err = service.Delete(context.TODO(), "test/abc.txt")
if err != nil {
  log.Fatal(err)
}

Transforming Images

store = storage.New(service, nil)

// transform image "sample.jpg" to a new jpeg with quality 75 and size 100x100 
options := storage.VariantOptions{}.
  SetFormat("jpeg").
  SetSize(100).
  SetQuality(75)
variant := store.Variant("sample.jpg", options)
err = variant.Process() // generate variant automatically if variant not exits
if err != nil {
  log.Fatal(err)
}
key := variant.Key()
// variants/sample-9c1aec99c27e6c66d895519f9ef831da.jpeg

Serve files and variants

server.Handler will redirect to the actual service endpoint. This indirection decouples the service URL from the actual one.

server := storage.NewServer("http://127.0.0.1:8080/storage/redirect", store, nil, nil)
url := server.URL("sample.jpg", nil)
// http://127.0.0.1:8080/storage/redirect?key=sample.jpg
variantURL := server.URL("sample.jpg", options)
// http://127.0.0.1:8080/storage/redirect?key=sample.jpg&format=jpeg&size=100&quality=75
// accessing variantURL will generate variant automatically if variant not exits

http.Handle("/storage/redirect", server.Handler())
err = http.ListenAndServe(":8080", nil)
if err != nil {
  panic(err)
}

# Packages

No description provided by the author

# Functions

No description provided by the author
New creates a new storage.
NewDiskService creates a new disk service.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewServer creates a new server.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RedirectURL returns serving URL of the variant.
ServeDisk serves files from the given directory.
No description provided by the author
No description provided by the author
No description provided by the author
WithS3ContentType set s3 object content-type for upload and copy.
WithS3Private set s3 object acl to private for upload and copy.
WithS3PublicRead set s3 object acl to public-read for upload and copy.
WithURLExpires sets the expires of the serving URL.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author