blob

package
v0.0.0-...-8e84a60 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 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", 0644, WithFlags(os.O_WRONLY | os.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", 0644, WithFlags(os.O_WRONLY | os.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

This section is empty.

Variables

This section is empty.

Functions

func WithFlags

func WithFlags(flags int) jsfs.OFOption

Flags sets the flags based on package "os" flag values. By default this is os.O_RDONLY.

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 New

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

New 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, perms fs.FileMode, options ...jsfs.OFOption) (fs.File, error)

OpenFile implements github.com/gopherfs/fs.OpenFilerFS. When creating a new file, this will always be a block blob.

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