bfs

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2018 License: Apache-2.0 Imports: 7 Imported by: 7

README

BFS

GoDoc Build Status Go Report Card

Multi-adapter bucket-based file system abstraction.

Documentation

For documentation and examples, please see https://godoc.org/github.com/bsm/bfs.

Install

go get -u github.com/bsm/bfs

Basic Usage

package main

import (
	"fmt"

	"github.com/bsm/bfs"
)

func main() {
	ctx := context.Background()
	bucket := bfs.NewInMem()

	o1, err := bucket.Create(ctx, "nested/file.txt")
	if err != nil {
		panic(err)
	}
	defer o1.Close()

	if _, err := o1.Write([]byte("TESTDATA")); err != nil {
		panic(err)
	}
	if err := o1.Close(); err != nil {
		panic(err)
	}

	entries, err := bucket.Glob(ctx, "nested/*")
	if err != nil {
		panic(err)
	}
	fmt.Println("ENTRIES:", entries)

	o2, err := bucket.Open(ctx, "nested/file.txt")
	if err != nil {
		panic(err)
	}
	defer o2.Close()

	data, err := ioutil.ReadAll(o2)
	if err != nil {
		panic(err)
	}
	fmt.Println("DATA:", string(data))

	info, err := bucket.Head(ctx, "nested/file.txt")
	if err != nil {
		panic(err)
	}
	fmt.Printf("INFO: name=%q size=%d\n", info.Name, info.Size)

	if err := bucket.Remove(ctx, "nested/file.txt"); err != nil {
		panic(err)
	}
}

Documentation

Overview

Package bfs outlines an abstraction for bucket-based fyle systems with mock-implmentations.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("bfs: object not found")

ErrNotFound must be returned by all implementations when a requested object cannot be found.

Functions

func CopyObject

func CopyObject(bucket Bucket, ctx context.Context, src, dst string) error

CopyObject is a quick helper to copy objects within the same bucket.

func WriteObject

func WriteObject(bucket Bucket, ctx context.Context, name string, data []byte) error

WriteObject is a quick write helper.

Types

type Bucket

type Bucket interface {
	// Glob lists the files mathing a glob pattern.
	Glob(ctx context.Context, pattern string) (Iterator, error)

	// Head returns an object's meta Info.
	Head(ctx context.Context, name string) (*MetaInfo, error)

	// Open opens an object for reading.
	Open(ctx context.Context, name string) (io.ReadCloser, error)

	// Create creates/opens a object for writing.
	Create(ctx context.Context, name string) (io.WriteCloser, error)

	// Remove removes a object.
	Remove(ctx context.Context, name string) error

	// Close closes the bucket.
	Close() error
}

Bucket is an abstract storage bucket.

type InMem

type InMem struct {
	// contains filtered or unexported fields
}

InMem is an in-memory Bucket implementation which can be used for mocking.

Example
package main

import (
	"context"
	"fmt"
	"io/ioutil"

	"github.com/bsm/bfs"
)

func main() {
	ctx := context.Background()
	bucket := bfs.NewInMem()

	// Write object
	o1, err := bucket.Create(ctx, "nested/file.txt")
	if err != nil {
		panic(err)
	}
	defer o1.Close()

	if _, err := o1.Write([]byte("TESTDATA")); err != nil {
		panic(err)
	}
	if err := o1.Close(); err != nil {
		panic(err)
	}

	// Glob entries
	entries, err := bucket.Glob(ctx, "nested/*")
	if err != nil {
		panic(err)
	}
	fmt.Println("ENTRIES:", entries)

	// Read object
	o2, err := bucket.Open(ctx, "nested/file.txt")
	if err != nil {
		panic(err)
	}
	defer o2.Close()

	data, err := ioutil.ReadAll(o2)
	if err != nil {
		panic(err)
	}
	fmt.Println("DATA:", string(data))

	// Head object
	info, err := bucket.Head(ctx, "nested/file.txt")
	if err != nil {
		panic(err)
	}
	fmt.Printf("INFO: name=%q size=%d\n", info.Name, info.Size)

	// Delete object
	if err := bucket.Remove(ctx, "nested/file.txt"); err != nil {
		panic(err)
	}
}

func NewInMem

func NewInMem() *InMem

NewInMem returns an initialised Bucket.

func (*InMem) Close

func (*InMem) Close() error

Close implements Bucket.

func (*InMem) Create

func (b *InMem) Create(_ context.Context, name string) (io.WriteCloser, error)

Create implements Bucket.

func (*InMem) Glob

func (b *InMem) Glob(_ context.Context, pattern string) (Iterator, error)

Glob implements Bucket.

func (*InMem) Head

func (b *InMem) Head(_ context.Context, name string) (*MetaInfo, error)

Head implements Bucket.

func (*InMem) ObjectSizes added in v0.2.1

func (b *InMem) ObjectSizes() map[string]int64

ObjectSizes return a map of object sizes by name

func (*InMem) Open

func (b *InMem) Open(_ context.Context, name string) (io.ReadCloser, error)

Open implements Bucket.

func (*InMem) Remove

func (b *InMem) Remove(_ context.Context, name string) error

Remove implements Bucket.

type Iterator added in v0.2.0

type Iterator interface {
	// Next advances the cursor to the next position.
	Next() bool
	// Name returns the name at the current cursor position.
	Name() string
	// Error returns the last iterator error, if any.
	Error() error
	// Close closes the iterator, should always be deferred.
	Close() error
}

Iterator iterates over objects

type MetaInfo

type MetaInfo struct {
	Name    string    // base name of the object
	Size    int64     // length of the content in bytes
	ModTime time.Time // modification time
}

MetaInfo contains meta information about an object.

Directories

Path Synopsis
bfsaz module
Package bfsfs abstracts local file system.
Package bfsfs abstracts local file system.
bfsftp module
Package bfsgs abstracts Google Cloud Storage bucket.
Package bfsgs abstracts Google Cloud Storage bucket.
Package bfss3 abstracts Amazon S3 bucket.
Package bfss3 abstracts Amazon S3 bucket.
bfsscp module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL