blob

package
v0.0.0-...-c4e7759 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package blob is an implementation of the io.FS for Azure blob storage. This package foresakes all the options offered by the standard azure storage package to simplify use. If you need options not provided here, your best solution is probably to use the standard package.

This package supports two additional features over io.FS capabilities: - Writing files opened with OpenFile() - Locking files

This currently only support Block Blobs, not Append or Page. We may offer that in the future with enough demand.

NOTE: NUMBER ONE MISTAKE: FORGETTING .CLOSE() on WRITING A FILE, SO IT DOES NOT WRITE THE FILE.

Open a Blob storage container:

cred, err := msi.Token(msi.SystemAssigned{})
if err != nil {
	panic(err)
}

fsys, err := NewFS("account", "container", *cred)
if err != nil {
	// Do something
}

Read an entire file:

file, err := fsys.Open("users/jdoak.json")
if err != nil {
	// Do something
}

// You could also do fsys.ReadFile() for simplicity.
b, err := io.ReadAll(file)
if err != nil {
	// Do something
}

fmt.Println(string(b))

Stream a file to stdout:

file, err := fsys.Open("users/jdoak.json")
if err != nil {
	// Do something
}

if _, err := io.Copy(os.Stdout, file); err != nil {
	// Do something
}

Copy a file:

src, err := os.Open("path/to/some/file")
if err != nil {
	// Do something
}

dst, err := fsys.OpenFile("path/to/place/content", O_WRONLY | O_CREATE)
if err != nil {
	// Do something
}

if _, err := io.Copy(dst, src); err != nil {
	// Do something
}

// The file is not actually written until the file is closed, so it is
// important to know if Close() had an error.
if err := dst.Close(); err != nil {
	// Do something
}

Write a string to a file:

file, err := fsys.OpenFile("users/jdoak.json", O_WRONLY | O_CREATE)
if err != nil {
	// Do something
}

if _, err := io.WriteString(file, `{"Name":"John Doak"}`); err != nil {
	// Do something
}

// The file is not actually written until the file is closed, so it is
// important to know if Close() had an error.
if err := file.Close(); err != nil {
	// Do something
}

Walk the file system and log all directories:

err := fs.WalkDir(
	fsys,
	".",
	func(path string, d fs.DirEntry, err error) error {
		if !d.IsDir() {
			return nil
		}
		log.Println("dir: ", path)
		return nil
	},
)
if err != nil {
	// Do something
}

Index

Constants

View Source
const (
	O_RDONLY int = syscall.O_RDONLY // open the file read-only.
	O_WRONLY int = syscall.O_WRONLY // open the file write-only.
	// The remaining values may be or'ed in to control behavior.
	//O_APPEND int = syscall.O_APPEND // append data to the file when writing.
	O_CREATE int = syscall.O_CREAT // create a new file if none exists.
	O_EXCL   int = syscall.O_EXCL  // used with O_CREATE, file must not exist.
	O_TRUNC  int = syscall.O_TRUNC // truncate regular writable file when opened.
)

These are a subset of what is in "os" that is supported by this file systm.

Variables

This section is empty.

Functions

func WithLock

func WithLock() jsfs.OFOption

WithLock locks the file and attempts to keep it locked until the file is closed. If the file in question is a directory, no lease it taken out.

func WithTransferManager

func WithTransferManager(tm azblob.TransferManager) jsfs.OFOption

WithTransferManager allows you to provide one of azblob's TransferManagers or your own TransferManager for controlling file writes.

Types

type FS

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

FS implements io/fs.FS

func NewFS

func NewFS(account, container string, cred azblob.Credential) (*FS, error)

NewFS is the constructor for FS. It is recommended that you use blob/auth/msi to create the "cred".

func (*FS) Open

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

Open implements fs.FS.Open().

func (*FS) OpenFile

func (f *FS) OpenFile(name string, flags int, options ...jsfs.OFOption) (fs.File, error)

OpenFile implements github.com/johnsiilver/fs.OpenFilerFS. When creating a new file, this will always be a block blob. The fs.File returned will always be a *File.

func (*FS) ReadDir

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

func (*FS) ReadFile

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

ReadFile implements fs.ReadFileFS.ReadFile.

func (*FS) Stat

func (f *FS) Stat(name string) (fs.FileInfo, error)

Stat implements fs.StatFS.Stat.

func (*FS) WriteFile

func (f *FS) WriteFile(name string, data []byte, perm fs.FileMode) error

WriteFile implements jsfs.Writer. This implementation takes a lock on each file. Use OpenFile() if you do not with to use locking or want to use other options.

type File

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

File implements io.FS.File and io.Writer for blobs.

func (*File) Close

func (f *File) Close() error

Close implements fs.File.Close().

func (*File) Read

func (f *File) Read(p []byte) (n int, err error)

Read implements fs.File.Read().

func (*File) ReadDir

func (f *File) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir implements fs.ReadDirFile.ReadDir().

func (*File) Stat

func (f *File) Stat() (fs.FileInfo, error)

Stat implements fs.File.Stat().

func (*File) Write

func (f *File) Write(p []byte) (n int, err error)

Write implements io.Writer.Write().

type Sys

type Sys struct {
	// Props holds propertis of the blobstore file.
	Props *azblob.BlobGetPropertiesResponse
}

Sys is returned on a FileInfo.Sys() call.

Directories

Path Synopsis
auth
msi
Package msi provides authentication methods using Microsoft Service Identities.
Package msi provides authentication methods using Microsoft Service Identities.

Jump to

Keyboard shortcuts

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