Skip to content

rovergulf/storage

Repository files navigation

GitHub go.mod Go version GitHub Actions Workflow Status

storage

This repository contains source code for file storage, supporting local file storage and cloud remote ones.

Install module

go get github.com/rovergulf/storage

Example

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
    }
}

OpenTelemetry support

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	
	}

}