filestorage

package
v0.0.0-...-44b9937 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2024 License: MIT Imports: 5 Imported by: 0

README

The real

cloud.google.com/go/storage and google.golang.org/api/option packages are what production code will eventually use. But, during dev time it's nice to just write files to the hard drive of your one free tier google compute instance. You get 30 GB for free! And storing in a real bucket will cost some money.

func getStorageClient() (*filestorage.Client, string) {
	keyPath := ""
	client, err := filestorage.NewClient(context.Background(),
		option.WithCredentialsFile(keyPath))
	if err != nil {
		return nil, ""
	}
	client.BucketPath = "/bucket"
	bucket := "unique-name"
	return client, bucket
}

This is the same make client code you'll need but it's using this fake filestorage package which does everything the real client does but uses your machine's hard drive.

Write:

	client, bucket := getStorageClient()
	w := client.Bucket(bucket).Object(filename).NewWriter(context.Background())
	w.ContentType = "application/octet-stream"
	w.Write(data)
	w.Close()

Delete:

	client, bucket := getStorageClient(c)
	ctx := context.Background()
	client.Bucket(bucket).Object(object).Delete(ctx)

How much space is left:

func getAvailableDiskSpace(path string) (uint64, error) {
	var stat syscall.Statfs_t

	err := syscall.Statfs(path, &stat)
	if err != nil {
		return 0, err
	}

	availableSpace := stat.Bavail * uint64(stat.Bsize)
	return availableSpace, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	BucketPath string
}

func (*Bucket) Object

func (b *Bucket) Object(s string) *Object

type Client

type Client struct {
	BucketPath string
}

func NewClient

func NewClient(ctx context.Context, option option.ClientOption) (*Client, error)

func (*Client) Bucket

func (c *Client) Bucket(s string) *Bucket

type Object

type Object struct {
	Filename   string
	BucketPath string
}

func (*Object) Delete

func (o *Object) Delete(ctx context.Context)

func (*Object) NewReader

func (o *Object) NewReader(ctx context.Context) (*Reader, error)

func (*Object) NewWriter

func (o *Object) NewWriter(ctx context.Context) *Writer

type Reader

type Reader struct {
	Filename   string
	BucketPath string
	// contains filtered or unexported fields
}

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

type Writer

type Writer struct {
	ContentType string
	Filename    string
	BucketPath  string
}

func (*Writer) Close

func (w *Writer) Close()

func (*Writer) Write

func (w *Writer) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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