Storage
File storage with local filesystem and Amazon S3 drivers.
RapidGo provides a unified file storage interface with local and S3 backends.
Configuration
STORAGE_DRIVER=local # local, s3
# Local driver
STORAGE_LOCAL_PATH=storage/uploads
# S3 driver
S3_BUCKET=my-bucket
S3_REGION=us-east-1
S3_KEY=your-access-key
S3_SECRET=your-secret-key
Storage Driver Interface
type Driver interface {
Put(path string, content io.Reader) (string, error)
Get(path string) (io.ReadCloser, error)
Delete(path string) error
URL(path string) string
}
Creating a Driver
import "github.com/RAiWorks/RapidGo/v2/core/storage"
// Auto-detect from STORAGE_DRIVER env
driver, err := storage.NewDriver()
Usage
// Upload a file
file, _ := c.FormFile("avatar")
f, _ := file.Open()
defer f.Close()
path, err := driver.Put("avatars/user-42.jpg", f)
// Get a file
reader, err := driver.Get("avatars/user-42.jpg")
defer reader.Close()
io.Copy(w, reader)
// Get public URL
url := driver.URL("avatars/user-42.jpg")
// Local: "/uploads/avatars/user-42.jpg"
// S3: "https://my-bucket.s3.us-east-1.amazonaws.com/avatars/user-42.jpg"
// Delete a file
driver.Delete("avatars/user-42.jpg")
Local Driver
Stores files in the local filesystem (default: storage/uploads/). The URL() method returns a path relative to your static file server.
S3 Driver
Stores files in Amazon S3. Requires S3_BUCKET, S3_REGION, S3_KEY, and S3_SECRET environment variables.