blobstore

package module
v0.0.0-...-8304448 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

Blobstore for MongoDB

This is intended to be a simple replacement for binary object storage in MongoDB. GridFS does not play well with transactions, and it looks too much like a file system so using it for generic BLOB storage is usually inconvenient. This Go library provides a simple replacement for it.

Create a blob store using:

store := &blobstore.Store {
   Collection: coll,
}

Make sure indexes are created for the store. This will run once for the lifetime of the store.

store.EnsureIndex(context.Background())

You can simple write to a BLOB with its id. You have to generate an ID yourself:

	err := store.Write(context.Background(), blobID, bytes.NewReader(data))

And read from it:

	rd, err := store.Read(context.Background(), blobID)

You have to close the returned reader. You can close it midway if you are not interested in the whole stream.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultChunkSize = 2048 * 1024 // 2MB chunks
View Source
var ErrNotFound = errors.New("Not found")

Functions

This section is empty.

Types

type Store

type Store struct {
	Collection *mongo.Collection
	ChunkSize  int
	// contains filtered or unexported fields
}

func (*Store) EnsureIndex

func (store *Store) EnsureIndex(ctx context.Context) (err error)

EnsureIndex ensures that the collection has an index on id and seq. This can be called multiple times on a store object.

func (*Store) Read

func (store *Store) Read(ctx context.Context, blobID string) (io.ReadCloser, error)

Read blob data. To stop reading, close the returned readCloser. You must close the returned stream, otherwise the goroutine streaming the data will leak.

func (*Store) Remove

func (store *Store) Remove(ctx context.Context, blobIDs ...string) error

Remove all given blobs

func (*Store) Size

func (store *Store) Size(ctx context.Context, blobID string) (int64, error)

Size returns the size of the object

func (*Store) Write

func (store *Store) Write(ctx context.Context, blobID string, data io.Reader) error

Write blob data. Data can be nil, if so, a truncated blob will be written

Jump to

Keyboard shortcuts

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