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 ¶
- func WithFlags(flags int) jsfs.OFOption
- 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, perms fs.FileMode, 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 ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithFlags ¶
Flags sets the flags based on package "os" flag values. By default this is os.O_RDONLY.
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 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) OpenFile ¶
OpenFile implements github.com/gopherfs/fs.OpenFilerFS. When creating a new file, this will always be a block blob.
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.