package gridfs
import "go.mongodb.org/mongo-driver/mongo/gridfs"
Index ¶
- Constants
- Variables
- type Bucket
- func NewBucket(db *mongo.Database, opts ...*options.BucketOptions) (*Bucket, error)
- func (b *Bucket) Delete(fileID primitive.ObjectID) error
- func (b *Bucket) DownloadToStream(fileID primitive.ObjectID, stream io.Writer) (int64, error)
- func (b *Bucket) DownloadToStreamByName(filename string, stream io.Writer, opts ...*options.NameOptions) (int64, error)
- func (b *Bucket) Drop() error
- func (b *Bucket) Find(filter interface{}, opts ...*options.GridFSFindOptions) (mongo.Cursor, error)
- func (b *Bucket) OpenDownloadStream(fileID primitive.ObjectID) (*DownloadStream, error)
- func (b *Bucket) OpenDownloadStreamByName(filename string, opts ...*options.NameOptions) (*DownloadStream, error)
- func (b *Bucket) OpenUploadStream(filename string, opts ...*options.UploadOptions) (*UploadStream, error)
- func (b *Bucket) OpenUploadStreamWithID(fileID primitive.ObjectID, filename string, opts ...*options.UploadOptions) (*UploadStream, error)
- func (b *Bucket) Rename(fileID primitive.ObjectID, newFilename string) error
- func (b *Bucket) SetReadDeadline(t time.Time) error
- func (b *Bucket) SetWriteDeadline(t time.Time) error
- func (b *Bucket) UploadFromStream(filename string, source io.Reader, opts ...*options.UploadOptions) (primitive.ObjectID, error)
- func (b *Bucket) UploadFromStreamWithID(fileID primitive.ObjectID, filename string, source io.Reader, opts ...*options.UploadOptions) error
- type DownloadStream
- func (ds *DownloadStream) Close() error
- func (ds *DownloadStream) Read(p []byte) (int, error)
- func (ds *DownloadStream) SetReadDeadline(t time.Time) error
- func (ds *DownloadStream) Skip(skip int64) (int64, error)
- type Upload
- type UploadStream
Constants ¶
const DefaultChunkSize int32 = 255 * 1000 // 255 KB
DefaultChunkSize is the default size of each file chunk.
const UploadBufferSize = 16 * 1000000 // 16 MB
UploadBufferSize is the size in bytes of one stream batch. Chunks will be written to the db after the sum of chunk lengths is equal to the batch size.
Variables ¶
ErrFileNotFound occurs if a user asks to download a file with a file ID that isn't found in the files collection.
ErrStreamClosed is an error returned if an operation is attempted on a closed/aborted stream.
ErrWrongIndex is used when the chunk retrieved from the server does not have the expected index.
ErrWrongSize is used when the chunk retrieved from the server does not have the expected size.
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket represents a GridFS bucket.
func NewBucket ¶
NewBucket creates a GridFS bucket.
func (*Bucket) Delete ¶
Delete deletes all chunks and metadata associated with the file with the given file ID.
func (*Bucket) DownloadToStream ¶
DownloadToStream downloads the file with the specified fileID and writes it to the provided io.Writer. Returns the number of bytes written to the steam and an error, or nil if there was no error.
func (*Bucket) DownloadToStreamByName ¶
func (b *Bucket) DownloadToStreamByName(filename string, stream io.Writer, opts ...*options.NameOptions) (int64, error)
DownloadToStreamByName downloads the file with the given name to the given io.Writer.
func (*Bucket) Drop ¶
Drop drops the files and chunks collections associated with this bucket.
func (*Bucket) Find ¶
Find returns the files collection documents that match the given filter.
func (*Bucket) OpenDownloadStream ¶
func (b *Bucket) OpenDownloadStream(fileID primitive.ObjectID) (*DownloadStream, error)
OpenDownloadStream creates a stream from which the contents of the file can be read.
func (*Bucket) OpenDownloadStreamByName ¶
func (b *Bucket) OpenDownloadStreamByName(filename string, opts ...*options.NameOptions) (*DownloadStream, error)
OpenDownloadStreamByName opens a download stream for the file with the given filename.
func (*Bucket) OpenUploadStream ¶
func (b *Bucket) OpenUploadStream(filename string, opts ...*options.UploadOptions) (*UploadStream, error)
OpenUploadStream creates a file ID new upload stream for a file given the filename.
func (*Bucket) OpenUploadStreamWithID ¶
func (b *Bucket) OpenUploadStreamWithID(fileID primitive.ObjectID, filename string, opts ...*options.UploadOptions) (*UploadStream, error)
OpenUploadStreamWithID creates a new upload stream for a file given the file ID and filename.
func (*Bucket) Rename ¶
Rename renames the stored file with the specified file ID.
func (*Bucket) SetReadDeadline ¶
SetReadDeadline sets the read deadline for this bucket
func (*Bucket) SetWriteDeadline ¶
SetWriteDeadline sets the write deadline for this bucket.
func (*Bucket) UploadFromStream ¶
func (b *Bucket) UploadFromStream(filename string, source io.Reader, opts ...*options.UploadOptions) (primitive.ObjectID, error)
UploadFromStream creates a fileID and uploads a file given a source stream.
func (*Bucket) UploadFromStreamWithID ¶
func (b *Bucket) UploadFromStreamWithID(fileID primitive.ObjectID, filename string, source io.Reader, opts ...*options.UploadOptions) error
UploadFromStreamWithID uploads a file given a source stream.
type DownloadStream ¶
type DownloadStream struct {
// contains filtered or unexported fields
}
DownloadStream is a io.Reader that can be used to download a file from a GridFS bucket.
func (*DownloadStream) Close ¶
func (ds *DownloadStream) Close() error
Close closes this download stream.
func (*DownloadStream) Read ¶
func (ds *DownloadStream) Read(p []byte) (int, error)
Read reads the file from the server and writes it to a destination byte slice.
func (*DownloadStream) SetReadDeadline ¶
func (ds *DownloadStream) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline for this download stream.
func (*DownloadStream) Skip ¶
func (ds *DownloadStream) Skip(skip int64) (int64, error)
Skip skips a given number of bytes in the file.
type Upload ¶
type Upload struct {
// contains filtered or unexported fields
}
Upload contains options to upload a file to a bucket.
type UploadStream ¶
type UploadStream struct { *Upload // chunk size and metadata FileID primitive.ObjectID // contains filtered or unexported fields }
UploadStream is used to upload files in chunks.
func (*UploadStream) Abort ¶
func (us *UploadStream) Abort() error
Abort closes the stream and deletes all file chunks that have already been written.
func (*UploadStream) Close ¶
func (us *UploadStream) Close() error
Close closes this upload stream.
func (*UploadStream) SetWriteDeadline ¶
func (us *UploadStream) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline for this stream.
func (*UploadStream) Write ¶
func (us *UploadStream) Write(p []byte) (int, error)
Write transfers the contents of a byte slice into this upload stream. If the stream's underlying buffer fills up, the buffer will be uploaded as chunks to the server. Implements the io.Writer interface.
Source Files ¶
bucket.go download_stream.go upload_stream.go
- Version
- v0.2.0
- Published
- Jan 8, 2019
- Platform
- js/wasm
- Imports
- 14 packages
- Last checked
- 51 minutes ago –
Tools for package owners.