DefaultChunkSize is the default size of each file chunk.
const DefaultChunkSize int32 = 255 * 1024 // 255 KiB
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.
const UploadBufferSize = 16 * 1024 * 1024 // 16 MiB
ErrFileNotFound occurs if a user asks to download a file with a file ID that isn't found in the files collection.
var ErrFileNotFound = errors.New("file with given parameters not found")
ErrMissingChunkSize occurs when downloading a file if the files collection document is missing the "chunkSize" field.
var ErrMissingChunkSize = errors.New("files collection document does not contain a 'chunkSize' field")
ErrStreamClosed is an error returned if an operation is attempted on a closed/aborted stream.
var ErrStreamClosed = errors.New("stream is closed or aborted")
ErrWrongIndex is used when the chunk retrieved from the server does not have the expected index.
var ErrWrongIndex = errors.New("chunk index does not match expected index")
ErrWrongSize is used when the chunk retrieved from the server does not have the expected size.
var ErrWrongSize = errors.New("chunk size does not match expected size")
Bucket represents a GridFS bucket.
type Bucket struct {
// contains filtered or unexported fields
}
func NewBucket(db *mongo.Database, opts ...*options.BucketOptions) (*Bucket, error)
NewBucket creates a GridFS bucket.
func (b *Bucket) Delete(fileID interface{}) error
Delete deletes all chunks and metadata associated with the file with the given file ID.
If this operation requires a custom write deadline to be set on the bucket, it cannot be done concurrently with other write operations operations on this bucket that also require a custom deadline.
Use SetWriteDeadline to set a deadline for the delete operation.
▹ Example
func (b *Bucket) DeleteContext(ctx context.Context, fileID interface{}) error
DeleteContext deletes all chunks and metadata associated with the file with the given file ID and runs the underlying delete operations with the provided context.
Use the context parameter to time-out or cancel the delete operation. The deadline set by SetWriteDeadline is ignored.
func (b *Bucket) DownloadToStream(fileID interface{}, stream io.Writer) (int64, error)
DownloadToStream downloads the file with the specified fileID and writes it to the provided io.Writer. Returns the number of bytes written to the stream and an error, or nil if there was no error.
If this download requires a custom read deadline to be set on the bucket, it cannot be done concurrently with other read operations operations on this bucket that also require a custom deadline.
▹ Example
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.
If this download requires a custom read deadline to be set on the bucket, it cannot be done concurrently with other read operations operations on this bucket that also require a custom deadline.
func (b *Bucket) Drop() error
Drop drops the files and chunks collections associated with this bucket.
If this operation requires a custom write deadline to be set on the bucket, it cannot be done concurrently with other write operations operations on this bucket that also require a custom deadline
Use SetWriteDeadline to set a deadline for the drop operation.
▹ Example
func (b *Bucket) DropContext(ctx context.Context) error
DropContext drops the files and chunks collections associated with this bucket and runs the drop operations with the provided context.
Use the context parameter to time-out or cancel the drop operation. The deadline set by SetWriteDeadline is ignored.
func (b *Bucket) Find(filter interface{}, opts ...*options.GridFSFindOptions) (*mongo.Cursor, error)
Find returns the files collection documents that match the given filter.
If this download requires a custom read deadline to be set on the bucket, it cannot be done concurrently with other read operations operations on this bucket that also require a custom deadline.
Use SetReadDeadline to set a deadline for the find operation.
▹ Example
func (b *Bucket) FindContext(ctx context.Context, filter interface{}, opts ...*options.GridFSFindOptions) (*mongo.Cursor, error)
FindContext returns the files collection documents that match the given filter and runs the underlying find query with the provided context.
Use the context parameter to time-out or cancel the find operation. The deadline set by SetReadDeadline is ignored.
func (b *Bucket) GetChunksCollection() *mongo.Collection
GetChunksCollection returns a handle to the collection that stores the file chunks for this bucket.
func (b *Bucket) GetFilesCollection() *mongo.Collection
GetFilesCollection returns a handle to the collection that stores the file documents for this bucket.
func (b *Bucket) OpenDownloadStream(fileID interface{}) (*DownloadStream, error)
OpenDownloadStream creates a stream from which the contents of the file can be read.
▹ Example
func (b *Bucket) OpenDownloadStreamByName(filename string, opts ...*options.NameOptions) (*DownloadStream, error)
OpenDownloadStreamByName opens a download stream for the file with the given filename.
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.
▹ Example
func (b *Bucket) OpenUploadStreamWithID(fileID interface{}, filename string, opts ...*options.UploadOptions) (*UploadStream, error)
OpenUploadStreamWithID creates a new upload stream for a file given the file ID and filename.
func (b *Bucket) Rename(fileID interface{}, newFilename string) error
Rename renames the stored file with the specified file ID.
If this operation requires a custom write deadline to be set on the bucket, it cannot be done concurrently with other write operations operations on this bucket that also require a custom deadline
Use SetWriteDeadline to set a deadline for the rename operation.
▹ Example
func (b *Bucket) RenameContext(ctx context.Context, fileID interface{}, newFilename string) error
RenameContext renames the stored file with the specified file ID and runs the underlying update with the provided context.
Use the context parameter to time-out or cancel the rename operation. The deadline set by SetWriteDeadline is ignored.
func (b *Bucket) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline for this bucket
func (b *Bucket) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline for this bucket.
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.
If this upload requires a custom write deadline to be set on the bucket, it cannot be done concurrently with other write operations operations on this bucket that also require a custom deadline.
▹ Example
func (b *Bucket) UploadFromStreamWithID(fileID interface{}, filename string, source io.Reader, opts ...*options.UploadOptions) error
UploadFromStreamWithID uploads a file given a source stream.
If this upload requires a custom write deadline to be set on the bucket, it cannot be done concurrently with other write operations operations on this bucket that also require a custom deadline.
DownloadStream is a io.Reader that can be used to download a file from a GridFS bucket.
type DownloadStream struct {
// contains filtered or unexported fields
}
func (ds *DownloadStream) Close() error
Close closes this download stream.
func (ds *DownloadStream) GetFile() *File
GetFile returns a File object representing the file being downloaded.
func (ds *DownloadStream) Read(p []byte) (int, error)
Read reads the file from the server and writes it to a destination byte slice.
func (ds *DownloadStream) SetReadDeadline(t time.Time) error
SetReadDeadline sets the read deadline for this download stream.
func (ds *DownloadStream) Skip(skip int64) (int64, error)
Skip skips a given number of bytes in the file.
File represents a file stored in GridFS. This type can be used to access file information when downloading using the DownloadStream.GetFile method.
type File struct { // ID is the file's ID. This will match the file ID specified when uploading the file. If an upload helper that // does not require a file ID was used, this field will be a primitive.ObjectID. ID interface{} // Length is the length of this file in bytes. Length int64 // ChunkSize is the maximum number of bytes for each chunk in this file. ChunkSize int32 // UploadDate is the time this file was added to GridFS in UTC. This field is set by the driver and is not configurable. // The Metadata field can be used to store a custom date. UploadDate time.Time // Name is the name of this file. Name string // Metadata is additional data that was specified when creating this file. This field can be unmarshalled into a // custom type using the bson.Unmarshal family of functions. Metadata bson.Raw }
func (f *File) UnmarshalBSON(data []byte) error
UnmarshalBSON implements the bson.Unmarshaler interface.
Deprecated: Unmarshaling a File from BSON will not be supported in Go Driver 2.0.
Upload contains options to upload a file to a bucket.
type Upload struct {
// contains filtered or unexported fields
}
UploadStream is used to upload a file in chunks. This type implements the io.Writer interface and a file can be uploaded using the Write method. After an upload is complete, the Close method must be called to write file metadata.
type UploadStream struct { *Upload // chunk size and metadata FileID interface{} // contains filtered or unexported fields }
func (us *UploadStream) Abort() error
Abort closes the stream and deletes all file chunks that have already been written.
func (us *UploadStream) Close() error
Close writes file metadata to the files collection and cleans up any resources associated with the UploadStream.
func (us *UploadStream) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the write deadline for this stream.
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.