This repository contains source code for file storage, supporting local file storage and cloud remote ones.
go get github.com/rovergulf/storage
package main
import (
"context"
"log"
"github.com/rovergulf/storage"
)
func main() {
ctx := context.Background()
storagePath := "/temp/my-storage"
fs, err := storage.NewStorage(
storage.WithBackends(storage.LocalBackends),
storage.WithPrefix(storagePath),
)
if err != nil {
log.Fatal(err)
}
fileData := []byte(`{"example": "hello world"}`)
fileName := "example.json"
// check if file key already exists
if exists, _ := fs.Exists(ctx, fileName); exists {
log.Printf("'%s' already exists", fileName)
return
}
// upload data
uploadErr := fs.Put(ctx, fileName, fileData)
if uploadErr != nil {
log.Fatalf("Unable to upload to '%s': %s", fileName, err)
// handle uploadErr
}
}
This package supports OpenTelemetry tracer spans.
Visit opentelemetry-go examples to install tracer provider and provide tracer as storage storage.WithTracer
option
package main
import (
...
"go.opentelemetry.io/otel"
"github.com/rovergulf/storage"
)
func main() {
tracer := otel.Tracer("go-storage")
fs, err := storage.NewStorage(
...,
storage.WithTracer(tracer),
)
if err != nil {
// handle err
}
}