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
- func WithLock() jsfs.OFOption
- func WithTransferManager(tm azblob.TransferManager) jsfs.OFOption
- type FS
- func (f *FS) Open(name string) (fs.File, error)
- func (f *FS) OpenFile(name string, flags int, options ...jsfs.OFOption) (fs.File, error)
- func (f *FS) ReadDir(name string) ([]fs.DirEntry, error)
- func (f *FS) ReadFile(name string) ([]byte, error)
- func (f *FS) Stat(name string) (fs.FileInfo, error)
- func (f *FS) WriteFile(name string, data []byte, perm fs.FileMode) error
- type File
- type Sys
Constants ¶
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 ¶
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) OpenFile ¶
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.
type File ¶
type File struct {
// contains filtered or unexported fields
}
File implements io.FS.File and io.Writer for blobs.
type Sys ¶
type Sys struct { // Props holds propertis of the blobstore file. Props *azblob.BlobGetPropertiesResponse }
Sys is returned on a FileInfo.Sys() call.