blobfs

package module
v0.0.0-...-1e65785 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 7 Imported by: 0

README

blobfs

test Go Reference Codecov Go Report Card

Package blobfs provides access with fs.FS interface to files stored in a blob storage.

FS implements fs.FS, so it can be used with any packages that understands file system interfaces.

e.g.) net/http, text/template, html/template

It uses gocloud.dev/blob for a blob backend, so it can read the following blob storages.

  • Google Cloud Storage
  • Amazon S3
  • Azure Blob Storage
  • Local filesystem
  • In memory filesystem

For more details about gocloud.dev/blob, please refer to the following page. ref.) https://gocloud.dev/howto/blob/

Example

package blobfs_test

import (
	"context"
	"fmt"
	"io"
	"log"
	"os"

	"gocloud.dev/blob"
	_ "gocloud.dev/blob/fileblob"

	"github.com/ichizero/blobfs"
)

func Example_fileblob() {
	ctx := context.Background()
	dir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	bucket, err := blob.OpenBucket(ctx, fmt.Sprintf("file://%s/testdata", dir))
	if err != nil {
		log.Fatal(err)
	}

	fsys := blobfs.New(bucket)
	f, err := fsys.Open("foo.txt")
	b, err := io.ReadAll(f)
	if err != nil {
		if err != io.EOF {
			log.Fatal(err)
		}
	}
	log.Print(string(b))
}

Documentation

Overview

Package blobfs provides access with fs.FS interface to files stored in a blob storage.

FS implements fs.FS, so it can be used with any packages that understands file system interfaces. e.g.) net/http, text/template, html/template

It uses gocloud.dev/blob for a blob backend, so it can read the following blob storages.

- Google Cloud Storage - Amazon S3 - Azure Blob Storage - Local filesystem - In memory filesystem

For more details about gocloud.dev/blob, please refer to the following page. ref.) https://gocloud.dev/howto/blob/

Example (Fileblob)
package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"os"

	"gocloud.dev/blob"

	"github.com/ichizero/blobfs"
	_ "gocloud.dev/blob/fileblob"
)

func main() {
	ctx := context.Background()
	dir, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	bucket, err := blob.OpenBucket(ctx, fmt.Sprintf("file://%s/testdata", dir))
	if err != nil {
		log.Fatal(err)
	}

	fsys := blobfs.New(bucket)
	f, err := fsys.Open("foo.txt")
	if err != nil {
		log.Fatal(err)
	}
	b, err := io.ReadAll(f)
	if err != nil {
		if err != io.EOF {
			log.Fatal(err)
		}
	}
	log.Print(string(b))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FS

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

An FS is a read-only blob storage file system that implements fs.FS interface.

func New

func New(bucket *blob.Bucket) *FS

New returns FS object that can interact with a blob storage.

func (*FS) Open

func (fsys *FS) Open(name string) (fs.File, error)

Open opens the named file for reading and returns it as an fs.File.

func (*FS) ReadDir

func (fsys *FS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads and returns all entries in the named directory.

func (*FS) ReadFile

func (fsys *FS) ReadFile(name string) ([]byte, error)

ReadFile reads and returns the content of the named file.

Jump to

Keyboard shortcuts

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